C #: from medium to advance level [Classes and Constructors (Part II)]
First of all, what is a Constructor?
Constructor is a method that is call when we create an instance of the Class.
Why we have to use it?
Constructor are really useful because when we make an object, we put it in “early state”, or to be clear, we can initialize fields directly in the instant we create an object.
Let’s look how work
To declare a Constructor, this is the syntax
It is a method, so the syntax is nearly the same:
we have public, then we use the SAME NAME OF CLASS, and it has no type or it is not void, just public and name of the Class.
If we need to initialize something, we put the code inside parentheses.
And it can have parameters.
Here we have a PARAMETERLESS or DEFAULT Constructor, because we declare it but is empty.
If WE DON’T CREATE IT, by default C # will create one for us.
Don’t worry, that Constructor don’t do nothing, simply initialize the state of fields to default value.
What is means?
If we have a int by default are set to zero, if we have a bool by defaukt is set to false, and so on.
Now, let’s look at Constructor with parameters
We can see two variables name, one in Pascal case and the other in camel case.
As we can see, we use “this” keyword.
“this” is reference at current object, so “this.Name” is reference to public string Name.
With this constructor we simply pass a string parameter, and everytime we initialize (or instantiate) the Class, we need to pass a string, and the value we pass is simply copy to our Name field in the Class.
Another interesting topic is about CONSTRUCTOR OVERLOADING.
Overload means we can have a more than one method with same name, but with a different “signature”.
Let’s look at this example
I have three Constructor, same name but with some difference : first is empty, second needs a string, the third needs a string and an int.
And is perfectly fine, as we can see there are no errors.
The classic question is : why we need to overload Constructors?
Well, this make the initialization of our Classes more easy.
Sometimes we need only a name, sometimes we need both name and id, and we have both.
Or if we not need nothing, we use the empty Constructor.
I make some example in code to see how work Constructors.
I create an Enemy Class, we stay focus on Game things.
Just to be clear, I make every fields public until we arrive at the topic of Properties and because is more simple to explain some things, but this is really NOT A GOOD WAY to programming.
But for testing things, is completely ok.
And because we are good programmer, we move this Class in its own file.
Ok, in Main method of Program Class we create a new Enemy.
And we access to the fields and print them in Console.Writeline.
If we run Program now
We have an empty or null string, and a 0.
Because without starting value, string are empty and integer are set to zero.
Now adding some Constructors to our Enemy Class
If we back to Program, we see a red underline.
Because now we have two Constructors, but none of them are empty, so we need to pass at least one parameter.
This is important: since we have a constructor, C # didn’t create the empty one for us. So if we need the empty constructor, we need to create it ourselves.
If we try we see the result