Overloading the Constructor in C#

 

In general we used to see multiple methods having same name with different signatures(different number of parameters or difference in data types) and it is called Method Overloading. In the same we can overload the constructor in C#. Constructor defines with same name as class name, so if class contains multiple constructors with difference in signature is called Constructor overloading.

 

The signature of a method or constructor is defined by its name and its parameter list. Two methods differ in their signatures if they have different names or different parameter lists. As shown below we define the class Employee with default constructor.

 

public class Employee

{

        Employee()

        {

       

        }

}

 

Now we will add multiple constructors with same name and in difference in signature.

 

public class Employee

{

        Employee()

        {

       

        }

 

        Employee(int id)

        {

 

        }

 

        Employee(int id, string name)

        {

 

        }

}

 

Constructors does not have return type so you cannot overload the constructors with difference in return type.