Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Class Constructors | Introduction to Object-Oriented Programming (OOP)
C# Beyond Basics

Class ConstructorsClass Constructors

A class constructor is the same as a struct constructor. It is a method which is executed when an object is created.

The syntax for creating a constructor is the following:

cs

index.cs

For-example:

cs

index.cs

You can see from the output that the Console.WriteLine statement inside the Player constructor is executed every time a new object is created.

In C# 9 and older versions of C#, there are some differences between structs' and classes' constructors. For example, in older versions, you cannot created parameterless constructors for Structs, however, in C# 10 and newer versions it is supported. It is recommended to use the latest version of C# compilers otherwise you might face some errors while compiling some of the code.

Following is an example code from the Struct Constructors chapter. The term static is explained in the comments as it is more relevant here however it is not necessary to completely understand it as this concept will be explained in detail in later sections.

Unlike normal fields, a static field can hold data even if there's no object that class. Static fields have the same value for all objects therefore changing the value of a static field from one object changes it for all objects. The program class represents the program itself, therefore if the static field is a member of the 'Program' class then it acts like a global variable.
cs

index.cs

In order to convert the Player struct into a class, all we need to do is to change the term struct to class:

cs

index.cs

Similarly, we can have constructors with some arguments as well. Usually, this method is used to make the initialization of class fields easier:

cs

index.cs

At this point, we have covered most of the basic concepts of the Classes, and judging from the syntax and behavior, Classes seem to be exactly the same as Structs, however, that is not actually the case. As stated in the first chapter of this section, Structs are a limited version of Classes that only provide a basic functionality on the other hand the features of Classes are much more complex and broad. In this section we have covered all the features that are similar between Classes and Structs. In the next two sections we will learn all the intricacies of classes and object-oriented programming.

1. When is a constructor method called?
2. What will be the name of a constructor method in a class called "Animal"?
3. Which keyword is used before a constructor name?

When is a constructor method called?

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

What will be the name of a constructor method in a class called "Animal"?

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

Which keyword is used before a constructor name?

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

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

Секція 3. Розділ 9
course content

Зміст курсу

C# Beyond Basics

Class ConstructorsClass Constructors

A class constructor is the same as a struct constructor. It is a method which is executed when an object is created.

The syntax for creating a constructor is the following:

cs

index.cs

For-example:

cs

index.cs

You can see from the output that the Console.WriteLine statement inside the Player constructor is executed every time a new object is created.

In C# 9 and older versions of C#, there are some differences between structs' and classes' constructors. For example, in older versions, you cannot created parameterless constructors for Structs, however, in C# 10 and newer versions it is supported. It is recommended to use the latest version of C# compilers otherwise you might face some errors while compiling some of the code.

Following is an example code from the Struct Constructors chapter. The term static is explained in the comments as it is more relevant here however it is not necessary to completely understand it as this concept will be explained in detail in later sections.

Unlike normal fields, a static field can hold data even if there's no object that class. Static fields have the same value for all objects therefore changing the value of a static field from one object changes it for all objects. The program class represents the program itself, therefore if the static field is a member of the 'Program' class then it acts like a global variable.
cs

index.cs

In order to convert the Player struct into a class, all we need to do is to change the term struct to class:

cs

index.cs

Similarly, we can have constructors with some arguments as well. Usually, this method is used to make the initialization of class fields easier:

cs

index.cs

At this point, we have covered most of the basic concepts of the Classes, and judging from the syntax and behavior, Classes seem to be exactly the same as Structs, however, that is not actually the case. As stated in the first chapter of this section, Structs are a limited version of Classes that only provide a basic functionality on the other hand the features of Classes are much more complex and broad. In this section we have covered all the features that are similar between Classes and Structs. In the next two sections we will learn all the intricacies of classes and object-oriented programming.

1. When is a constructor method called?
2. What will be the name of a constructor method in a class called "Animal"?
3. Which keyword is used before a constructor name?

When is a constructor method called?

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

What will be the name of a constructor method in a class called "Animal"?

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

Which keyword is used before a constructor name?

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

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

Секція 3. Розділ 9
some-alt