Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Constructor Overloading in C#
Constructor overloading in C# allows you to define multiple constructors within the same class, each with different parameters. This provides flexibility in object initialization, allowing different ways to create instances of the same class.
Constructor overloading follows the same rules as method overloading − constructors must have different parameter lists (number, type, or order of parameters).
Syntax
Following is the syntax for constructor overloading −
public class ClassName {
public ClassName() {
// default constructor
}
public ClassName(int param) {
// constructor with one parameter
}
public ClassName(int param1, string param2) {
// constructor with two parameters
}
}
How Constructor Overloading Works
The compiler determines which constructor to call based on the arguments provided during object creation. Each constructor can initialize the object differently based on the parameters passed.
Example
The following example demonstrates constructor overloading with a Student class having three different constructors −
using System;
class Student {
private double SubjectOne;
private double SubjectTwo;
string StudentName;
public Student() {
this.SubjectOne = 80;
}
public Student(double SubjectOne) {
this.SubjectOne = SubjectOne;
}
public Student(string StudentName, double SubjectOne, double SubjectTwo) {
this.SubjectOne = SubjectOne;
this.SubjectTwo = SubjectTwo;
this.StudentName = StudentName;
}
public double GetSubjectOneMarks() {
return this.SubjectOne;
}
public double GetSubjectTwoMarks() {
return this.SubjectTwo;
}
public string GetStudentName() {
return this.StudentName;
}
}
class Program {
static void Main(string[] args) {
Student s1 = new Student();
Student s2 = new Student(90);
Student s3 = new Student("Amit", 88, 60);
Console.WriteLine("Student One");
Console.WriteLine("Subject One Marks: {0}", s1.GetSubjectOneMarks());
Console.WriteLine();
Console.WriteLine("Student Two");
Console.WriteLine("Subject One Marks: {0}", s2.GetSubjectOneMarks());
Console.WriteLine();
Console.WriteLine("Student Three");
Console.WriteLine("Student name: {0}", s3.GetStudentName());
Console.WriteLine("Subject One Marks: {0}", s3.GetSubjectOneMarks());
Console.WriteLine("Subject Two Marks: {0}", s3.GetSubjectTwoMarks());
}
}
The output of the above code is −
Student One Subject One Marks: 80 Student Two Subject One Marks: 90 Student Three Student name: Amit Subject One Marks: 88 Subject Two Marks: 60
Constructor Chaining with Overloading
You can combine constructor overloading with constructor chaining using the this keyword to avoid code duplication −
using System;
class Rectangle {
private double length;
private double width;
public Rectangle() : this(1.0, 1.0) {
Console.WriteLine("Default constructor called");
}
public Rectangle(double side) : this(side, side) {
Console.WriteLine("Square constructor called");
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
Console.WriteLine("Rectangle constructor called");
}
public double Area() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}, Width: {1}, Area: {2}", length, width, Area());
}
}
class Program {
static void Main(string[] args) {
Rectangle r1 = new Rectangle();
r1.Display();
Console.WriteLine();
Rectangle r2 = new Rectangle(5.0);
r2.Display();
Console.WriteLine();
Rectangle r3 = new Rectangle(4.0, 6.0);
r3.Display();
}
}
The output of the above code is −
Rectangle constructor called Default constructor called Length: 1, Width: 1, Area: 1 Rectangle constructor called Square constructor called Length: 5, Width: 5, Area: 25 Rectangle constructor called Length: 4, Width: 6, Area: 24
Key Rules for Constructor Overloading
-
Each overloaded constructor must have a unique parameter list (different number, type, or order of parameters).
-
Constructors cannot differ only by return type (constructors don't have return types).
-
The
thiskeyword can be used for constructor chaining to call one constructor from another. -
Access modifiers (public, private) can be different for each constructor.
Conclusion
Constructor overloading in C# provides multiple ways to initialize objects of the same class with different parameter combinations. This feature enhances code flexibility and allows for more intuitive object creation based on the available data at instantiation time.
