Like method overloading constructors can also be overloaded. Constructor overloading is a process of adding multiple constructors in class with different argument lists. The compiler takes the decision of which constructor is to be executed at the compile time depending upon the parameters passed to the constructor. Constructor overloading gives an opportunity to construct an object with different ways and initialize the data members depending upon the situations. Constructor overloading also helpful when you want to execute different statements depending upon the parameters passed to the constructor. Once you add the parameterized constructor in class the default constructor provided by C# is canceled and you have to write default constructor with zero arguments explicitly.

Example:

using System;
namespace ConstructorOverloading
{
class Demo
{
public int number1, number2;

public Demo()   //First constructor
{
number1 = 11;
number2 = 22;
}

public Demo(int value)  //Second constructor
{
number1 = value;
}

public Demo(int value1, int value2) //Third constructor
{
number1 = value1;
number2 = value2;
}
}

class Program
{
static void Main(string[] args)
{
Demo demo1 = new Demo();    //Executes first constructor
Console.WriteLine("First object data members:");
Console.WriteLine(demo1.number1);
Console.WriteLine(demo1.number2);
Demo demo2 = new Demo(55);  //Executes second constructor
Console.WriteLine("Second object data members:");
Console.WriteLine(demo2.number1);
Console.WriteLine(demo2.number2);
Demo demo3 = new Demo(88, 99);  //Executes third constructor
Console.WriteLine("Third object data members:");
Console.WriteLine(demo3.number1);
Console.WriteLine(demo3.number2);
Console.ReadLine();
}
}
}

The output of above program is:

First object data members:
11
22
Second object data members:
55
0
Third object data members:
88
99

In above example, in class Demo we write three different constructors with different arguments. When we create an object of Demo class with no parameters (object demo1) then the default constructor is executed and variables number1 and number2 is initialized with values 11 and 22 respectively in the constructor. In the creation of second object demo2, we pass only one parameter value 55 so compiler matches the parameter list and data type of parameter with constructors and calls the appropriate constructor and the variable number1 is initialized with passed value 55 and number2 is initialized with default value 0 in the constructor. When we pass two parameters 88 and 99, the third constructor gets executed and sets the variables number1 and number2 to values 88 and 99 respectively.

Recommended Post   C# Boxing and Unboxing

Using ‘this’ keyword to invoke constructor:

In the real programming world, there is sometimes the requirement to call one constructor from another constructor. In general constructor with matching parameters gets executed when a new object of a class is created. If we want to execute two or more constructors for the same object then we can do this by invoking one constructor from another.  To achieve this in C# ‘this’ keyword is used. The general form of is as below:

constructor_name (parameter_list):this(parameter_list)
{
	//Constructor statements 
}

Here this keyword refers to the constructor with matching parameter list. The parameter list in this keyword call must match with the parameters of the constructor which is you want to call. The constructor indicated by this keyword is executed first then the calling constructor is executed.

Note: You cannot invoke the same constructor from itself.

For example:

public Demo():this()
{

}

The above constructor is not allowed in C#. Here constructor trying to invoke itself which leads to recursion.

Example:

using System;
namespace ConstructorOverloading
{
class Demo
{
public int number1, number2;
public string str1;
public Demo():this("Constructor Demo")
{
Console.WriteLine("Executing first constructor.");
}
public Demo(string value) :this(10,20)
{
str1 = value;
Console.WriteLine("Executing second constructor.");
}
public Demo(int value1, int value2)
{
number1 = value1;
number2 = value2;
Console.WriteLine("Executing third constructor.");
}
}
class Program
{
static void Main(string[] args)
{
Demo demo1 = new Demo();
Console.WriteLine("The value of variable str1: {0}",demo1.str1);
Console.WriteLine("The value of variable number1: {0}", demo1.number1);
Console.WriteLine("The value of variable number2: {0}", demo1.number2);
Console.ReadLine();
}
}
}

 

The output of above program is:

Executing third constructor.
Executing second constructor.
Executing first constructor.
The value of variable str1: Constructor Demo
The value of variable number1: 10
The value of variable numbers: 20

 

In above example, we write three constructors with different parameters. From the first constructor, we invoke the second constructor and from second constructor we invoke the third constructor. When we create object demo1 then all three constructors gets executed. Look at the output the third constructor gets executed first and then second and then the first constructor is executed.

Recommended Post   Anonymous Classes and Methods in C#

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here