Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Class Constructors | Section
C# Object-Oriented Structures

bookClass 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:

index.cs

index.cs

copy
1234567
class className { // ... other class code public className() { // code } }

For example:

index.cs

index.cs

copy
1234567891011121314151617
using System; class Player { public Player() { Console.WriteLine("A new player is created"); } } public class HelloWorld { public static void Main(string[] args) { Player p1 = new Player(); Player p2 = new Player(); Player p3 = new Player(); } }

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

Note
Note

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.

Note
Note

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.

index.cs

index.cs

copy
1234567891011121314151617181920212223
using System; struct Program { public static int totalPlayers = 0; struct Player { public int id; public Player() { id = totalPlayers++; Console.WriteLine($"New Player Object Created With ID {id}"); } } static void Main(string[] args) { Player player1 = new Player(); Player player2 = new Player(); Player player3 = new Player(); } }

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

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728
using System; class Program { // Unlike normal fields, a 'static' field can hold data even if there's no object. // Static fields have the same value for all objects. // Changing the value of a static field from one object changes it for all objects. // In this case the static field is part of the 'Program' class which represents the program itself // Therefore this field is essentially a global variable public static int totalPlayers = 0; struct Player { public int id; public Player() { id = totalPlayers++; Console.WriteLine($"New Player Object Created With ID {id}"); } } static void Main(string[] args) { Player player1 = new Player(); Player player2 = new Player(); Player player3 = new Player(); } }

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

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728
using System; class Point { public double x; public double y; public Point(double x, double y) { // 'this' keyword is also used in classes just like how its used in structs this.x = x; this.y = y; } public void showCoordinates() { Console.WriteLine($"({x}, {y})"); } } class Program { static void Main(string[] args) { Point p1 = new Point(50, 70); Point p2 = new Point(70, 90); p1.showCoordinates(); p2.showCoordinates(); } }

At this point, you 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 you have covered all the features that are similar between Classes and Structs. In the next two sections you 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?

question mark

When is a constructor method called?

正しい答えを選んでください

question mark

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

正しい答えを選んでください

question mark

Which keyword is used before a constructor name?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  30

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  30
some-alt