In simple words it is the Constructor which is helping the object to be created.
There are two types of Constructor :
Say we are going to create two Human objects out of the above Human class. i.e. John from America and Wang Chu from China.
class Human{ string name; String food; String language; public void eat(){ System.Console.WriteLine("Eats "+food); } public void speak(){ System.Console.WriteLine("Speaks "+language); } } class CreateHuman { public static void Main(string[] args) { Human human = new Human(); } }
We have seen how to create an object of Human class using new keyword with the below statement :
Human human1 = new Human();
Although Human() looks like a normal C# method, but it has the same name as that of the class name(i.e Human). And that is what makes is different from a method and is called as Constructor which is helping the compiler to create a C# object.
But the strange fact is Human() is not present inside the above class. Then how is it helping the compiler to create an object?
The answer is, a constructor with no arguments is called as a default constructor. Even if you don't place it in the class, the compiler will automatically place it for you. Just wait a second, I just said a default constructor is a constructor with no arguments. So, that does mean there can be constructors with arguments. Yes, there are and we will have a detail look at it.
Lets modify the Human class with constructors:
Class Human { String name; String food; String language; Human() { } Human(String nme, String fd, String lang) { this.name = nme; this.food = fd; this.language = lang; } public void eat() { System.Console.WriteLine("Eats "+food); } public void speak() { System.Console.WriteLine("Speaks "+language); } }
So, in the above class we have two contructors. One with no arguments(default constructor), which the compiler inserted by default in the previous Human class.
Human() { }
And the other is the constructor with three arguments:
Human(String nme, String fd, String lang) { this.name = nme; this.food = fd; this.language = lang; }
class Human { string name; string food; string language; public Human() { } public Human(string nme, string fd, string lang) { name = nme; food = fd; language = lang; } public void eat(){ System.Console.WriteLine("Eats "+food); } public void speak(){ System.Console.WriteLine("Speaks "+language); } } class CreateHuman { public static void Main(string[] args) { Human human1 = new Human(); //Calls the no-argument/default Constructor Human human2 = new Human(); //Calls the no-argument/default Constructor Human argumentHuman1 = new Human("John","Burger","English"); //Calls the constructor with arguments Human argumentHuman2 = new Human("Wang Chu","Chowmein","Chinese"); //Calls the constructor with arguments argumentHuman1.eat(); argumentHuman1.speak(); argumentHuman2.eat(); argumentHuman2.speak(); } }
Lets see whats happens in the above code:
So, in the above image! name, food and language variables of human1 and human2 are not assigned any values. It is because human1 and human2 are created using the default constructor :
Human human1 = new Human(); Human human2 = new Human();
and no values are assigned to it.
Again let us relook at the below Image:
Again, in the above image argumentHuman1 and argumentHuman2 contains the values we have put in the constructor with arguments.
Let's have a look at the below code :
Human argumentHuman1 = new Human("John","Burger","English");
As we have already seen argumentHuman1 is the name of the object. And on the right hand side we have new which creates a new object named argumentHuman1.
Now, let's see what Human("John","Burger","English") does, relating with the below code(i.e. constructor with arguments).
public Human(string nme, string fd, string lang) { name = nme; food = fd; language = lang; }
So, "John" gets assigned to nme. i.e.
In the first line of the constructor the above gets assigned. Then if you see the lines inside the constructor
name = nme; food = fd; language = lang;
The value inside nme i.e "John" gets assigned to name.
Similarly, Burger is assigned to this.food, which is the food property of Human class and English is assigned to language property of Human class.