Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Struct Constructors | Structs & Enumerators
course content

Зміст курсу

C# Beyond Basics

Struct ConstructorsStruct Constructors

A constructor is a method which is automatically executed when a new object is created.

The syntax of a constructor is somewhat similar to that of a method, we simply omit the returnType as a constructor doesn't return any value:

cs

index.cs

The following points are important to note about the constructor syntax:

  1. The constructor name is the same as the structure name.
  2. A constructor does not have any return value.

The following program demonstrates how the constructor is called whenever an object is created:

cs

index.cs

In case you are using a C# version older than C# 10, you might get a compile-time error. It is recommended to get C# 10 or a later version. If you don't want to switch to a newer version then it is important to note that the usage of constructors will be more limited. For-example, you cannot create a parameterless constructor in older versions.

Let's add a field to Player called id which will be a unique identifier of that object so each object will have a different value for id. It will start from 0 and will increment. To achieve that we will create a global variable called totalPlayers.

cs

index.cs

In the above code, we placed the struct inside the Program class to be able to access the 'totalPlayers' variable from inside the constructor.

We can pass data into a constructor when creating a new object using the following syntax:

Following is a practical example of the usage:

cs

index.cs

Let's look at the code step by step.

First, we created a constructor and inside the constructor we assigned the passed values x, y, z to the fields x, y and z:

cs

index.cs

Inside the Main method, we created a new Coordinate3D object and passed 3, 5, and 7 as x, y and z through the constructor.

cs

index.cs

To confirm whether the fields were successfully initiated by the constructor or not, we used the displayValue method:

cs

index.cs

The output proved that the fields were successfully updated.

The constructors are very useful when we want to initiate objects with some data or for performing some initial operations when an object is created.

When are constructors called?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 8
course content

Зміст курсу

C# Beyond Basics

Struct ConstructorsStruct Constructors

A constructor is a method which is automatically executed when a new object is created.

The syntax of a constructor is somewhat similar to that of a method, we simply omit the returnType as a constructor doesn't return any value:

cs

index.cs

The following points are important to note about the constructor syntax:

  1. The constructor name is the same as the structure name.
  2. A constructor does not have any return value.

The following program demonstrates how the constructor is called whenever an object is created:

cs

index.cs

In case you are using a C# version older than C# 10, you might get a compile-time error. It is recommended to get C# 10 or a later version. If you don't want to switch to a newer version then it is important to note that the usage of constructors will be more limited. For-example, you cannot create a parameterless constructor in older versions.

Let's add a field to Player called id which will be a unique identifier of that object so each object will have a different value for id. It will start from 0 and will increment. To achieve that we will create a global variable called totalPlayers.

cs

index.cs

In the above code, we placed the struct inside the Program class to be able to access the 'totalPlayers' variable from inside the constructor.

We can pass data into a constructor when creating a new object using the following syntax:

Following is a practical example of the usage:

cs

index.cs

Let's look at the code step by step.

First, we created a constructor and inside the constructor we assigned the passed values x, y, z to the fields x, y and z:

cs

index.cs

Inside the Main method, we created a new Coordinate3D object and passed 3, 5, and 7 as x, y and z through the constructor.

cs

index.cs

To confirm whether the fields were successfully initiated by the constructor or not, we used the displayValue method:

cs

index.cs

The output proved that the fields were successfully updated.

The constructors are very useful when we want to initiate objects with some data or for performing some initial operations when an object is created.

When are constructors called?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 8
some-alt