1. What is Constructor Chaining?
Constructor chaining is a mechanism in C# where one constructor calls another constructor to reuse initialization logic.
Constructor chaining can happen in two ways:
-
this()– Calls another constructor within the same class. -
base()– Calls a constructor from the parent (base) class.
Constructor chaining helps avoid duplicate code and ensures objects are initialized in the correct order.
2. Constructor Chaining Using this()
What is this()?
The this() keyword is used to call another constructor within the same class.
Purpose:
-
Avoid duplicate initialization code.
-
Reuse existing constructors.
-
Provide multiple ways to initialize an object.
-
Maintain initialization logic in one place.
Example Program
using System;
public class Employee
{
public int Id;
public string Name;
public string Department;
// Constructor 1
public Employee() : this(101, "Unknown")
{
Console.WriteLine("Default Constructor");
}
// Constructor 2
public Employee(int id, string name)
: this(id, name, "IT")
{
Console.WriteLine("Two-Parameter Constructor");
}
// Constructor 3
public Employee(int id, string name, string department)
{
Id = id;
Name = name;
Department = department;
Console.WriteLine("Three-Parameter Constructor");
}
}
class Program
{
static void Main()
{
Employee emp = new Employee();
}
}
Execution Flow
new Employee()
|
v
Employee()
|
| this(101, "Unknown")
v
Employee(int id, string name)
|
| this(id, name, "IT")
v
Employee(int id, string name, string department)
|
| Initialize fields
| Print "Three-Parameter Constructor"
v
Return to Two-Parameter Constructor
|
| Print "Two-Parameter Constructor"
v
Return to Default Constructor
|
| Print "Default Constructor"
v
Object Created
Output
Three-Parameter Constructor
Two-Parameter Constructor
Default Constructor
Step-by-Step Explanation
Step 1: The following statement creates an Employee object:
Employee emp = new Employee();
Step 2: The parameterless constructor is selected.
public Employee() : this(101, "Unknown")
Instead of executing its body immediately, it calls the two-parameter constructor.
Step 3: The two-parameter constructor executes its initializer:
public Employee(int id, string name)
: this(id, name, "IT")
It calls the three-parameter constructor.
Step 4: The three-parameter constructor initializes the fields:
Id = id;
Name = name;
Department = department;
Step 5: Execution returns to the two-parameter constructor, followed by the parameterless constructor.
Final values:
Id = 101
Name = Unknown
Department = IT
Key Points – this()
-
this()calls another constructor in the same class. -
It supports constructor overloading.
-
The called constructor executes before the calling constructor's body.
-
It helps eliminate duplicate initialization logic.
-
Recursive or circular constructor chaining is not allowed.
-
Constructor initializers must appear before the constructor body, using
: this(...).
3. Constructor Chaining Using base()
What is base()?
The base() keyword is used to call a constructor of the parent class from a derived class.
It is commonly used in inheritance.
Purpose:
-
Initialize fields or properties defined in the parent class.
-
Reuse initialization logic from the base class.
-
Ensure the base class is properly initialized before the derived class constructor body executes.
Example Program
Consider a banking application where SavingsAccount inherits from BankAccount.
using System;
public class BankAccount
{
public int AccountNumber;
public string CustomerName;
// Base class constructor
public BankAccount(int accountNumber, string customerName)
{
AccountNumber = accountNumber;
CustomerName = customerName;
Console.WriteLine("BankAccount Constructor");
}
}
public class SavingsAccount : BankAccount
{
public decimal InterestRate;
// Derived class constructor
public SavingsAccount(
int accountNumber,
string customerName,
decimal interestRate)
: base(accountNumber, customerName)
{
InterestRate = interestRate;
Console.WriteLine("SavingsAccount Constructor");
}
}
class Program
{
static void Main()
{
SavingsAccount account = new SavingsAccount(
1001,
"John",
4.5m
);
Console.WriteLine($"Account Number: {account.AccountNumber}");
Console.WriteLine($"Customer Name: {account.CustomerName}");
Console.WriteLine($"Interest Rate: {account.InterestRate}");
}
}
Execution Flow
new SavingsAccount(1001, "John", 4.5m)
|
v
SavingsAccount Constructor Selected
|
| base(1001, "John")
v
BankAccount Constructor
|
| AccountNumber = 1001
| CustomerName = "John"
|
| Print "BankAccount Constructor"
v
Return to SavingsAccount Constructor
|
| InterestRate = 4.5
|
| Print "SavingsAccount Constructor"
v
SavingsAccount Object Created
Output
BankAccount Constructor
SavingsAccount Constructor
Account Number: 1001
Customer Name: John
Interest Rate: 4.5
Step-by-Step Explanation
Step 1: Create the derived class object.
SavingsAccount account =
new SavingsAccount(1001, "John", 4.5m);
Step 2: The derived constructor uses base().
: base(accountNumber, customerName)
This explicitly invokes the parent class constructor.
Step 3: The BankAccount constructor initializes:
AccountNumber = accountNumber;
CustomerName = customerName;
Step 4: After the base constructor finishes, the derived constructor body executes.
InterestRate = interestRate;
Step 5: The SavingsAccount object is ready to use.
Key Points – base()
-
base()calls a constructor in the immediate parent class. -
It is used with inheritance.
-
A base class constructor executes before the derived class constructor body.
-
If the base class has no accessible parameterless constructor, the derived constructor must explicitly invoke an accessible base constructor.
-
Constructors are not inherited, but derived constructors can invoke base constructors.
-
base()helps ensure common initialization is handled by the parent class.
4. Difference Between this() and base()
|
Feature |
|
|
|---|---|---|
|
Purpose |
Calls another constructor in the same class |
Calls a parent class constructor |
|
Used for |
Constructor overloading |
Inheritance |
|
Constructor location |
Same class |
Immediate base class |
|
Main benefit |
Avoid duplicate code |
Reuse base initialization |
|
Inheritance required |
No |
Yes |
|
Can pass parameters |
Yes |
Yes |
|
Execution |
Called constructor body runs first |
Base constructor body runs first |
5. Can We Use Both this() and base() Together?
Yes, both can participate in the same constructor chain, but not in the same constructor initializer.
For example:
public Employee() : this(101)
{
}
public Employee(int id) : base(id)
{
}
Here, the first constructor uses this(), and the second constructor uses base().
Complete Example
using System;
public class Person
{
public Person(string name)
{
Console.WriteLine($"Person Constructor: {name}");
}
}
public class Employee : Person
{
public Employee() : this("John")
{
Console.WriteLine("Employee Default Constructor");
}
public Employee(string name) : base(name)
{
Console.WriteLine("Employee Parameterized Constructor");
}
}
class Program
{
static void Main()
{
Employee emp = new Employee();
}
}
Execution Flow
new Employee()
|
v
Employee()
|
| this("John")
v
Employee(string name)
|
| base("John")
v
Person(string name)
|
| Print Person Constructor
v
Employee(string name) Body
|
| Print Parameterized Constructor
v
Employee() Body
|
| Print Default Constructor
v
Object Created
Output
Person Constructor: John
Employee Parameterized Constructor
Employee Default Constructor
Important: This is valid:
public Employee() : this("John")
{
}
But this is invalid:
// Compilation error
public Employee() : this("John"), base("John")
{
}
A constructor can have only one constructor initializer: either this(...) or base(...).
6. Important Interview Scenarios
Q1. What happens if we don't specify base()?
If no constructor initializer is specified, C# implicitly attempts to call the parameterless base constructor.
public class Parent
{
public Parent()
{
Console.WriteLine("Parent");
}
}
public class Child : Parent
{
public Child()
{
Console.WriteLine("Child");
}
}
Output:
Parent
Child
The compiler implicitly treats the Child constructor as though it were declared with : base().
Q2. What happens if the base class has only a parameterized constructor?
public class Parent
{
public Parent(int id)
{
}
}
public class Child : Parent
{
public Child() // Compilation error
{
}
}
The compiler cannot find an accessible parameterless constructor in Parent.
Solution:
public class Child : Parent
{
public Child() : base(100)
{
}
}
Q3. Can we call this() or base() inside the constructor body?
No. Constructor chaining uses the constructor initializer syntax.
public Employee() : this(100)
{
}
You cannot invoke a constructor as an ordinary method inside the body using this(100) or base(100).
Q4. Can a constructor call itself using this()?
No. Circular constructor chaining causes a compilation error.
public class Test
{
public Test() : this(10)
{
}
public Test(int id) : this()
{
}
}
The constructors form a circular dependency, so the compiler rejects them.
Q5. Does the base class constructor always execute before the derived class constructor?
The base constructor body executes before the derived constructor body.
However, there is an important distinction: instance field initializers in the derived class can execute before the base constructor body. Therefore, saying that all derived-class initialization occurs after base construction is not strictly correct.
7. Final Key Points for Interviews
-
Constructor chaining allows constructors to invoke other constructors during object initialization.
-
this()invokes another constructor within the same class. -
base()invokes a constructor in the immediate parent class. -
Both support passing arguments.
-
Only one constructor initializer is allowed per constructor.
-
Constructor chaining reduces duplicate initialization code.
-
When
base()is omitted, the compiler attempts an implicit parameterless base constructor call, unless the constructor explicitly chains usingthis(). -
Circular constructor chaining is prohibited.
-
Constructors cannot be overridden or inherited.
-
In ASP.NET Core,
base()is frequently used in custom base controllers, exception classes, and inheritance-based frameworks.
Interview Answer
Constructor chaining in C# is the process of calling one constructor from another constructor. We use this() to call another constructor within the same class and base() to call a constructor in the parent class. It helps reuse initialization logic, avoid code duplication, and initialize objects correctly. Both can participate in the same constructor chain, but a constructor can specify only one initializer.