C# | Constructor Overloading

Prerequisite: Constructors in C#

It is quite similar to the Method Overloading. It is the ability to redefine a Constructor in more than one form. A user can implement constructor overloading by defining two or more constructors in a class sharing the same name. C# can distinguish the constructors with different signatures. i.e. the constructor must have the same name but with different parameters list.

We can overload constructors in different ways as follows:

  • By using different type of arguments
  • By using different number of arguments
  • By using different order of arguments

By changing the Data types of the parameters

Example:



public ADD (int a, float b);
public ADD (string a, int b);

Here the name of the class is ADD. In first constructor there are two parameters, first one is int and another one is float and in second constructor, also there is two parameters, first one is string type and another one is int type.
Here the constructors have the same name but the types of the parameters are different, similar to the concept of method overloading.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Demonstrate the overloading of 
// constructor when the types of arguments 
// are different
using System;
  
class ADD {
      
    int x, y;
    double f;
    string s;
  
    // 1st constructor
    public ADD(int a, double b)
    {
        x = a;
        f = b;
    }
  
    // 2nd constructor
    public ADD(int a, string b)
    {
        y = a;
        s = b;
    }
  
    // showing 1st constructor's result
    public void show()
    {
        Console.WriteLine("1st constructor (int + float): {0} ",
                                                       (x + f));
    }
  
    // shows 2nd constructor's result
    public void show1()
    {
        Console.WriteLine("2nd constructor (int + string): {0}"
                                                       (s + y));
    }
}
  
// Driver Class
class GFG {
      
    // Main Method
    static void Main()
    {
          
        // Creating instance and
        // passing arguments
        // It will call the first constructor
        ADD g = new ADD(10, 20.2);
      
        // calling the method
        g.show();
  
  
        // Creating instance and 
        // passing arguments
        // It will call the second constructor
        ADD q = new ADD(10, "Roll No. is ");
      
        // calling the method
        q.show1();
    }
}

chevron_right


Output:

1st constructor (int + float): 30.2 
2nd constructor (int + string): Roll No. is 10

By changing the number of the parameters

In this case, we will use two or more constructors having the different number of parameters. The data types of arguments can be the same but the number of parameters will be different.

Example:

public ADD (int a, int b);
public ADD (int a, int b, int c);

Here, the class name is ADD. In the first constructor the number of parameter is two and the types of the parameters is int. In second constructor the number of parameter is three and the types of the parameters are also int, their is no problem with the data types.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate the overloading of 
// constructor when the number of parameters 
// are different
using System;
  
class ADD {
      
    int x, y;
    int f, p, s;
  
    // 1st constructor
    public ADD(int a, int b)
    {
        x = a;
        y = b;
    }
  
    // 2nd constructor
    public ADD(int a, int b, int c)
    {
        f = a;
        p = b;
        s = c;
    }
  
    // showing 1st constructor's result
    public void show()
    {
        Console.WriteLine("1st constructor (int + int): {0} "
                                                     (x + y));
    }
  
    // showing 2nd constructor's result
    public void show1()
    {
        Console.WriteLine("2nd constructor (int + int + int): {0}"
                                                      (f + p + s));
    }
}
  
// Driver Class
class GFG {
      
    // Main Method
    static void Main()
    {
          
        // will call 1st constructor
        ADD g = new ADD(10, 20);
          
        // calling method
        g.show();
  
        // will call 2nd constructor
        ADD q = new ADD(10, 20, 30);
      
        // calling method
        q.show1();
          
    }
}

chevron_right


Output:

1st constructor (int + int): 30 
2nd constructor (int + int + int): 60

By changing the Order of the parameters

Example:

public student(double a, int x, string s)
public student(string s, int x, double a)

Here, the two constructor hold the same types of parameters, that is, each constructor has one double type, one int type and one string type parameter, but the positions of the parameters are different. The compiler will invoke the constructor according to their argument order.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the overloading of
// constructor by changing the order of parameters
using System;
  
class student {
      
    public int roll;
    public double Height;
    public string name;
  
    public student(double a, int x, string s)
    {
        roll = x;
        name = s;
        Height = a;
    }
  
    // order of the argument is different
    // with respect to 1st constructor
    public student(string s, int x, double a)
    {
        Height = a;
        roll = x;
        name = s;
    }
  
    public void show()
    {
        Console.WriteLine("Roll Number: " + roll);
        Console.WriteLine("Height: " + Height + "feet");
        Console.WriteLine("Name: " + name);
    }
}
  
// Driver Class
class Geeks {
      
    // Main Method
    static void Main()
    {
          
        // invoking 1st constructor
        student s1 = new student(5.7, 10, "Jhon Peterson");
          
        // invoking 2nd constructor
        student s2 = new student("Peter Perker", 11, 6.0);
          
        Console.WriteLine("First Constructor: ");
        s1.show();
  
        Console.WriteLine();
  
        Console.WriteLine("Second Constructor: ");
        s2.show();
          
    }
}

chevron_right


Output:

First Constructor: 
Roll Number: 10
Height: 5.7feet
Name: Jhon Peterson

Second Constructor: 
Roll Number: 11
Height: 6feet
Name: Peter Perker

Invoke an Overloaded Constructor using “this” keyword

We can call an overloaded constructor from another constructor using this keyword but the constructor must be belong to the same class, because this keyword is pointing the members of same class in which this is used. This type of calling the overloaded constructor also termed as Constructor Chaining.


Example:

Let the class name is gfg,
Now
public gfg()
public gfg(int a) : this()
public gfg(double b) : this(int)

Here the first constructor is default constructor, second and third constructor are parameterized Constructor, where one has int type and another one has double type parameter.

In second constructor, this() invoke the first constructor which is default constructor. Here, after this keyword there is only ( ) which means the compiler invoke constructor that has no argument, means default constructor.

In third constructor, this(int) invoke the second constructor which is parameterized constructor. Here, after this there is (int) which means the compiler invoke constructor that has int type argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the invoking of
// overloaded constructor using this keyword
using System;
  
class GFG {
      
    // Private data members
    private int Length, Height;
    private double Width;
  
    // Default Constructor
    public GFG()
    {
        Console.WriteLine("Default Constructor Invoked");
    }
  
    // The constructor calls the
    // Default constructor
    public GFG(int l, double w) : this()
    {
        Console.WriteLine("Parameterized Constructor in 2nd Constructor");
          
        // assigning value of 
        // 'Length'and 'Width'
        Length = l;
        Width = w;
          
    }
  
    // The constructor call the
    // parameterized constructor
    public GFG(int l, double w, int h) : this(l, w)
    {
        Console.WriteLine("Parameterized Constructor in 3rd Constructor");
          
        // assign value of 'Height'
        Height = h;
      
    }
      
    // Public Method to calculate volume
    public double Volume()
    {
        return (Length * Width * Height);
    }
}
  
// Driver Class
class Geeks {
      
    // Main Method
    public static void Main()
    {
          
        // Invoking 3rd Constructor
        // here Constructor chaining
        // came into existence
        GFG g = new GFG(10, 20.5, 30);
  
        // calling the 'Volume' Method
        Console.WriteLine("Volume is : {0}", g.Volume());
  
    }
}

chevron_right


Output:

Default Constructor Invoked
Parameterized Constructor in 2nd Constructor
Parameterized Constructor in 3rd Constructor
Volume is : 6150

Overloading of Copy Constructor

A parameterized constructor that contains a parameter of same class type is called a copy constructor. Basically, copy constructor is a constructor which copies a data of one object into another object. Its main use is to initialize a new instance to the values of an existing instance.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// overloading of Copy Constructor
using System;
  
class GFG {
      
    public string p1, p2;
    public int p3, p4;
  
    // 1st Constructor
    public GFG(string x, string y)
    {
        p1 = x;
        p2 = y;
    }
  
    // Copy Constructor of 1st Constructor
    public GFG(GFG gf)
    {
        p1 = gf.p1;
        p2 = gf.p2;
    }
  
    // 2nd Constructor with different
    // types pf parameter
    public GFG(int a, int b)
    {
        p3 = a;
        p4 = b;
    }
  
    // Copy Constructor of 2nd Constructor
    // Here number of parameter is different
    // with respect to 1st Constructor
    public GFG(GFG a, GFG b)
    {
        p3 = a.p3;
        p4 = b.p4;
    }
}
  
// Driver Class
class Geeks {
      
// Main Method
static void Main()
{
      
    // Create instance to class 'GFG'
    GFG g = new GFG("Welcome", "GeeksForGeeks");
      
    // Here 'g' details will copied to 'g1'
    GFG g1 = new GFG(g);
  
    Console.WriteLine(g1.p1 + " to " + g1.p2);
  
    // Create instance to class 'GFG' 
    // with different types of parameter
    GFG G = new GFG(10, 20);
      
    // Here 'G' details will copied to 'G1'
    GFG G1 = new GFG(G, G);
  
    Console.WriteLine("Overloaded values : {0} and {1}"
                                          G1.p3, G1.p4);
  
}
}

chevron_right


Output:

Welcome to GeeksForGeeks
Overloaded values : 10 and 20

Note:

  • Static Constructor cannot be overload, because Static Constructors are parameterless constructor, but for overloading, we must need parameterized constructor.
  • Private Constructor cannot be overload, because of its protection level. Private members cannot be accessed from outside the class.


My Personal Notes arrow_drop_up

3rd year student of Information Technology JADAVPUR UNIVERSITY

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.