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
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.
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.
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.
1st constructor (int + int): 30 2nd constructor (int + int + int): 60
By changing the Order of the parameters
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.
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.
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.
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.
Welcome to GeeksForGeeks Overloaded values : 10 and 20
- Static Constructor cannot be overload, because Static Constructors are parameterless constructor, but for overloading, we must need parameterized constructor.
- Private Constructor can be overloaded and we can use that by instance of this class inside the same class. Private members cannot be accessed from outside the class.