Constructor overloading in C Sharp code example

Learn constructor overloading in C# with simple coding example – In C# programming, constructor overloading means, a class can have more than one constructors with different number of parameters or with different data types.

We have seen a example of a class having multiple constructors in C# programs. In fact, this is the constructor overloading that comes under compile time polymorphism in C# oops.

Let’s understand by example, what does it mean that having constructors with different number of parameters or with different data types with same number of arguments.

Constructors having different number of parameters

In below class, these two constructors have different number of parameters.
A(int a, int b){}
A(int a ,int b, double c){}

Constructors having different data types with same number of parameters

Below two constructors have same number of parameters i.e. one, but, with different data types.
A(int a){}
A(double b){}

Given class.

//Constructors overloading example
class A{
	
	//empty constructor
	A(){}
	
	/*constructors with different data types with same number of argument*/
	A(int a){}
	A(double b){}
	
	/*constructors with with different number of arguments*/
	A(int a, int b){}
	A(int a ,int b, double c){}
	
}

Example of Constructor overloading in C#

In below program, three Employee constructor will be overloaded with different number of signatures. When different objects with different parameters will be created then respective constructors will be called.

/*
  * Program for C# constructor overloading
  * */

    class Employee
    {
        private int id;
        private String name;

        /*----------------------------------------
         * Three overloaded constructors of the class
         */
        // Empty constructor
        public Employee()
        {
            this.id = 0;
            this.name = "Not Available";
        }

        // Overloaded constructor with int parameter
        public Employee(int id)
        {
            this.id = id;
            this.name = "Not Available";
        }

        // Overloaded constructor with a int parameter and a string
        public Employee(int id, String name)
        {
            this.id = id;
            this.name = name;
        }

        /*----------------------------------------
         * Display class fields of the class
         */

        public void display() {
 
		Console.WriteLine("Employee Info: ");
		Console.WriteLine("ID: " + this.id);
        Console.WriteLine("Name: " + this.name);
	}

    }

    /*----------------------------------------
     * Test program for constructor overloading in C#
     */

    public class ConstructorOverloading
    {

        public static void Main(String[] args)
        {
            // Call empty constructor
            Employee e1 = new Employee();
            e1.display();

            // Call one parameter constructor
            Employee e2 = new Employee(123);
            e2.display();

            // Call two parameters constructor
            Employee e3 = new Employee(123, "John");
            e3.display();

        }

    }

Output:

Employee Info:
ID: 0
Name: Not Available
Employee Info:
ID: 123
Name: Not Available
Employee Info:
ID: 123
Name: John

NOTE:

1)When an object of a class in C# program is created then a constructor is called implicitly as constructor cannot be called explicitly. So, in above constructor overloading program example, respective overloaded constructors will be called at the time of creation of objects of the class Employee i.e. e1, e2 and e3.

2) Also, note that constructor overloading is compile time polymorphism as similar to C# method overloading

3)In C# constructor chaining is different concept than constructor overloading.

Related Posts