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
How can we call one constructor from another in the same class in C#?
In C#, you can call one constructor from another constructor within the same class using the this keyword. This technique is called constructor chaining. To call a constructor from a parent class, use the base keyword.
Syntax
Following is the syntax for calling another constructor in the same class using this −
public ClassName(parameters) : this(arguments) {
// additional initialization code
}
Following is the syntax for calling a parent class constructor using base −
public DerivedClass(parameters) : base(arguments) {
// derived class initialization code
}
Using 'this' for Constructor Chaining
Constructor chaining allows you to reuse initialization logic by calling one constructor from another. The chained constructor executes first, followed by the calling constructor's body −
Example
using System;
class Demo {
public Demo() {
Console.WriteLine("Parameter less constructor called");
}
public Demo(int firstNumber, int secondNumber) : this() {
Console.WriteLine($"{firstNumber} {secondNumber}");
}
public Demo(int firstNumber, int secondNumber, int thirdNumber) : this(firstNumber, secondNumber) {
Console.WriteLine($"{firstNumber} {secondNumber} {thirdNumber}");
}
}
class Program {
static void Main() {
Demo obj = new Demo(1, 2, 3);
}
}
The output of the above code is −
Parameter less constructor called 1 2 1 2 3
Using 'base' for Parent Class Constructor
When a class inherits from another class, you can call the parent class constructor using the base keyword. The base constructor executes before the derived class constructor −
Example
using System;
class DemoBase {
public DemoBase(int firstNumber, int secondNumber, int thirdNumber) {
Console.WriteLine("Base class Constructor");
Console.WriteLine($"{firstNumber} {secondNumber} {thirdNumber}");
}
}
class Demo : DemoBase {
public Demo(int firstNumber, int secondNumber, int thirdNumber) : base(firstNumber, secondNumber, thirdNumber) {
Console.WriteLine("Derived class Constructor");
Console.WriteLine($"{firstNumber} {secondNumber} {thirdNumber}");
}
}
class Program {
static void Main() {
Demo obj = new Demo(1, 2, 3);
}
}
The output of the above code is −
Base class Constructor 1 2 3 Derived class Constructor 1 2 3
Combining 'this' and 'base' Keywords
You can combine both techniques in a class hierarchy where derived classes use constructor chaining −
Example
using System;
class Vehicle {
protected string type;
public Vehicle(string vehicleType) {
type = vehicleType;
Console.WriteLine($"Vehicle constructor: {type}");
}
}
class Car : Vehicle {
public Car() : this("Sedan") {
Console.WriteLine("Default car constructor");
}
public Car(string carType) : base($"Car - {carType}") {
Console.WriteLine($"Parameterized car constructor: {carType}");
}
}
class Program {
static void Main() {
Car defaultCar = new Car();
Console.WriteLine("---");
Car customCar = new Car("SUV");
}
}
The output of the above code is −
Vehicle constructor: Car - Sedan Parameterized car constructor: Sedan Default car constructor --- Vehicle constructor: Car - SUV Parameterized car constructor: SUV
Conclusion
Constructor chaining in C# allows you to call one constructor from another using this for same-class constructors and base for parent class constructors. This promotes code reuse and helps maintain consistent initialization logic across different constructor overloads.
