Sunday, January 16, 2011

Constructor

Constructor
Constructor is a special method without any return type and have same name as class name. Constructor is used to initialize an object of a class and it runs or executes automatically when you create an object. Constructors are called only once per creation of an object while regular methods can be called many times. Constructor can be private, public, static and protected. We can also overload constructor.

In c# constructor are called using new keyword.
Car objcar = new Car();
When you use new keyword then object is created and runtime will construct that object by using the definition of the class. Then it grab a piece of memory and fills with the fields (variable or methods) defined by the class and then a constructor is invoked to perform any initialization required.

Constructors are generally of following types
  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor

Default Constructor
When you create class without any constructor then compiler will provide you parameterless constructor with no body that parameterless constructor is called default constructor.

To explain default constructor we will make two program, in first program we will use default constructor which is provided by compiler and secondly we will create our own default constructor . 

We can't see default Constructor provided by compiler (but it is present in the class) thats why we will make our own default constructor to show that both are same.

First Program

You can see that we have two variables in class Name and LastName but not constructor but when we create an object of class its working properly. That means there is a default constructor in class provided by compiler but we can't see it.

Second Program

In this program we have two variables in class Name, LastName and a default constructor. When we will create an object the result will be the same as first program.
  
Parameterized Constructor
Constructor that accepts arguments when object is created is called parameterized constructor. Parameterized constructor is not provided by compiler you have to create this constructor. You can use parameterized constructor when you have to initialize data members of a class when object is created.


You can see Human constructor with two parameters Name and Lname. When we create an object we have to provide parameters (e.g. Human objH = new Human("James","Bond");) then Name = James and LastName = Bond will be assigned.

Copy Constructor
Copying values of one object to another object is called copy constructor. If you want to copy values of existing object to another new created object then you have to write method yourself because c# does not provide copy constructor.



We have create two object objH and objAnotherHuman, and will copy data of objH to objAnotherHuman. First we have create objH object and pass parameter(James, Bond) . It means that objH have Name = James and LastName = bond. Now we will create another object objAnotherHuman and will pass objH as parameter . Now copy constructor will copy obj.Name(i.e James) to objAnotherHuman.Name and obj.LastName(i.e Bond) to objAnotherHuman.

If you do not understand what happens or how values are assign in copy constructor then run the program in debugging mode 


Read about Private Constructor, Constructor Overloading, Constructor Chaining

How To call Constructor from another Constructor

No comments:

Post a Comment