Conteúdo do Curso
C# Beyond Basics
C# Beyond Basics
Class 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
class className { // ... other class code public className() { // code } }
For-example:
index
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.
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.
index
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 we need to do is to change the term struct
to class
:
index
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, we can have constructors with some arguments as well. Usually, this method is used to make the initialization of class fields easier:
index
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, 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.
Obrigado pelo seu feedback!