Pages

Wednesday, September 21, 2011

Constructor Chaining in CSharp


We starts with basics....
Basically ,it is a mehtod in the calss which executed when its object is created. We put the initialization code in the constructor. Creating a constructor in the class is pretty simple.
look at the following sample :

public class mySampleClass
{

    public mySampleClass()
    {

    // This is the constructor method.
    }
// rest of the class members goes here.
}

When the object of this class is instantiated this constructor will be executed automatically .

Like this :

// At this time the code in the constructor will // be executed
mySampleClass obj = new mySampleClass()


Constructor Overloading :


C# supports overloading of constructors, that means we can have constructors with different set of parameters. So our class can be like this :

public class mySampleClass
{

    public mySampleClass()
    {

    // no parameter constructor method.// First Constructor
    }
    public mySampleClass(int Age)
    {

    // constructor with one parameter.// Second Constructor
    }

    public mySampleClass(int Age, string Name)
    {

    // constructor with two parameters.// Third Constructor
    }

// rest of the class members goes here.
}

Well, note here that call to the constructor now depends on the way you instantiate the object.

For example :

mySampleClass obj = new mySampleClass()
// At this time the code of no parameter // constructor (First Constructor) will be executed

mySampleClass obj = new mySampleClass(12)
// At this time the code of one parameter // constructor(Second Constructor) will be // executed.
The call to the constructors is completely governed by the rules of the overloading here.

Calling Constructor from another Constructor:
You can always make the call to one constructor from within the other.
Say for example :
public
class mySampleClass
{

    public mySampleClass(): this(10)
    {

    // No parameter constructor method.// First Constructor
    }

    public mySampleClass(int Age)
    {

    // Constructor with one parameter.// Second Constructor}
    }

}

Very first of all let us see what is this syntax :
    

public mySampleClass(): this(10)

Here this refers to same class, so when we say this(10), we actually mean execute the public SampleClass(int Age) method.The above way of calling the method is called initializer. We can have at the most one initializer in this way in the method.
Another thing which we must know is the execution sequence i.e., which method will be executed when. Here if I instantiate the object as 

mySampleClass obj = new mySampleClass()
Then the code of public mySampleClass(int Age) will be executed before the code of mySampleClass(). So practically the definition of the method : 

public mySampleClass(): this(10)
{

    // This is the no parameter constructor method.// First Constructor
}

is equivalent to :
public mySampleClass()
{
    mySampleClass(10)

     // This is the no parameter constructor method.// First Constructor
}

This is sometimes called Constructor chaining.

3 comments: