Contenido del 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
For-example:
index
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
In order to convert the Player
struct into a class, all we need to do is to change the term struct
to class
:
index
Similarly, we can have constructors with some arguments as well. Usually, this method is used to make the initialization of class fields easier:
index
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.
¿Todo estuvo claro?