Constructor Chaining In C#

Basically, it is a method in the class 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:

  1. public class mySampleClass  
  2. {  
  3.     public mySampleClass()  
  4.     {  
  5.     // This is the constructor method.  
  6.     }  
  7. // rest of the class members goes here.  
  8. }   
When the object of this class is instantiated this constructor will be executed automatically.

Like this:
  1. // At this time the code in the constructor will // be executed  
  2. 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 the following code snippet:
  1. public class mySampleClass  
  2. {  
  3.     public mySampleClass()  
  4.     {  
  5.     // no parameter constructor method.// First Constructor  
  6.     }  
  7.     public mySampleClass(int Age)  
  8.     {  
  9.     // constructor with one parameter.// Second Constructor  
  10.     }  
  12.     public mySampleClass(int Age, string Name)  
  13.     {  
  14.     // constructor with two parameters.// Third Constructor  
  15.     }  
  17. // rest of the class members goes here.  
  18. }   
Well, note here that call to the constructor now depends on the way you instantiate the object.

For example:
  1. mySampleClass obj = new mySampleClass()  
  2. // At this time the code of no parameter // constructor (First Constructor) will be executed  
  4. mySampleClass obj = new mySampleClass(12)  
  5. // 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:
  1. public class mySampleClass  
  2. {  
  3.     public mySampleClass(): this(10)  
  4.     {  
  5.     // No parameter constructor method.// First Constructor  
  6.     }  
  8.     public mySampleClass(int Age)   
  9.     {  
  10.     // Constructor with one parameter.// Second Constructor}  
  11.     }  
  12. }  
First of all let us see what is this syntax:


    public mySampleClass(): this(10)

Here this refers to the 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 initialize 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:

  1. 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:
  1. public mySampleClass(): this(10)  
  2. {  
  3.    // This is the no parameter constructor method.// First Constructor  
  4. }   
is equivalent to:
  1. public mySampleClass()  
  2. {  
  3.     mySampleClass(10)  
  4.      // This is the no parameter constructor method.// First Constructor  
  5. }   
This is sometimes called Constructor chaining.