Constructor Chaining In C#

Introduction

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

    // Constructor
    public MySampleClass() {
        // This is the constructor method.
    }

    // Rest of the class members go here.

}

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

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

Constructor Overloading in C#

C# supports the overloading of constructors, which means we can have constructors with different sets of parameters. So our class can be like the following code snippet.

public class MySampleClass {

    // Constructor 1: no parameter constructor method
    public MySampleClass() {
        // First Constructor
    }

    // Constructor 2: constructor with one parameter
    public MySampleClass(int Age) {
        // Second Constructor
    }

    // Constructor 3: constructor with two parameters
    public MySampleClass(int Age, String Name) {
        // Third Constructor
    }

    // Rest of the class members go here.

}

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

Example

// Creating an object using the no parameter constructor
MySampleClass obj1 = new MySampleClass();
// At this time, the code of no parameter constructor (First Constructor) will be executed
// Creating an object using the constructor with one parameter
MySampleClass obj2 = 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 a Constructor from another Constructor

You can always make the call to one constructor from within the other.

Example

public class MySampleClass {

    // Constructor 1: no parameter constructor method, calls the second constructor
    public MySampleClass() {
        this(10); // Call the second constructor with a default value
        // First Constructor
    }

    // Constructor 2: constructor with one parameter
    public MySampleClass(int Age) {
        // Second Constructor
    }

}

First of all, let us see what this syntax is.

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 that we must know is the execution sequence, i.e., which method will be executed here if I instantiate the object.

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.


Similar Articles