Constructor Chaining in .NET: Calling Constructors from Other Constructors
Last Updated: Nov 05, 2025
5 min read
Legacy Archive
Legacy Guidance:This article preserves historical web development content. For modern .NET 8+ best practices, visit our Tutorials section.
Yes, you can call a constructor from another constructor using the this keyword. This technique, called constructor chaining, helps you avoid duplicating initialization code across multiple constructors in the same class.
Basic Constructor Chaining Example
Here's a simple example that demonstrates how to chain constructors together. The two-parameter constructor calls the one-parameter constructor, which sets up the initial values:
Constructor Chaining with this Keyword
class SampleClass
{
int member1;
string member2;
// Constructor with one parameter
public SampleClass(int data)
{
member1 = data;
}
// Constructor with two parameters, calling the first constructor
public SampleClass(int data, string strValue) : this(data)
{
member2 = strValue;
}
public void GetData()
{
Console.WriteLine("Member1: " + member1 + " Member2: " + member2);
}
}
class TestClass
{
public static void Main()
{
SampleClass obj = new SampleClass(10, "Good Day!");
obj.GetData();
}
}
When you run this code, the output will be:
Output
Member1: 10 Member2: Good Day!
How Constructor Chaining Works
When you create an instance of SampleClass by calling the two-argument constructor, the execution follows this order. First, the two-argument constructor uses : this(data) to call the one-argument constructor. The one-argument constructor runs and sets the value for member1. Control returns to the two-argument constructor, which then assigns the value to member2.
This approach keeps your initialization logic organized and prevents you from repeating the same code in multiple constructors.
Multiple Constructor Overloads
You can create several constructors with different parameter combinations, each calling another constructor. This creates a chain where more complex constructors build on simpler ones:
Multiple Constructor Chain
class Employee
{
int id;
string name;
string department;
double salary;
// Default constructor
public Employee() : this(0)
{
}
// Constructor with ID
public Employee(int empId) : this(empId, "Unknown")
{
}
// Constructor with ID and name
public Employee(int empId, string empName) : this(empId, empName, "General")
{
}
// Full constructor
public Employee(int empId, string empName, string dept)
{
id = empId;
name = empName;
department = dept;
salary = 0.0;
}
public void DisplayInfo()
{
Console.WriteLine($"ID: {id}, Name: {name}, Dept: {department}");
}
}
This example shows how each constructor adds more detail, with all of them eventually calling the most complete constructor that does the actual initialization work.
Practical Use Cases
Constructor chaining is particularly useful when you need to provide multiple ways to create objects. You might have default values for some properties, optional parameters, or different initialization scenarios. Rather than duplicating validation or setup code, you can centralize it in one constructor.
Practical Example with Validation
class Product
{
int productId;
string productName;
decimal price;
// Main constructor with validation
public Product(int id, string name, decimal productPrice)
{
if (id <= 0)
throw new ArgumentException("ID must be positive");
if (string.IsNullOrEmpty(name))
throw new ArgumentException("Name cannot be empty");
if (productPrice < 0)
throw new ArgumentException("Price cannot be negative");
productId = id;
productName = name;
price = productPrice;
}
// Simplified constructor with default price
public Product(int id, string name) : this(id, name, 0.0m)
{
}
// Display method
public void Display()
{
Console.WriteLine($"{productName} (ID: {productId}): ${price}");
}
}
In this example, the validation logic appears only once in the main constructor. All other constructors chain to it, ensuring every object gets properly validated regardless of which constructor you use.
Best Practices
When using constructor chaining, put your most complete constructor at the end of the chain. This constructor should contain all the actual initialization logic. Simpler constructors should just provide default values and call the more complete constructor.
Avoid creating circular references where constructors call each other in a loop. The compiler will catch this, but it's better to design your constructors properly from the start. Keep the chain simple and linear, flowing from simple to complex constructors.
Constructor chaining works well with optional parameters too. Sometimes you can replace multiple overloaded constructors with a single constructor that has optional parameters, but chaining remains useful when you need to perform different initialization logic based on which parameters are provided.
FAQ
Why would I want to chain constructors together?
Constructor chaining reduces code duplication by centralizing initialization logic. Instead of repeating the same initialization code in multiple constructors, you write it once and call it from other constructors. This makes your code easier to maintain and update.
Can I call a base class constructor and another constructor in the same class?
You can use either 'this' to call another constructor in the same class or 'base' to call a parent class constructor, but not both in the same constructor signature. Choose one approach based on your initialization needs.
What happens if I create a circular reference between constructors?
The C# compiler prevents circular constructor references and will show a compilation error. You cannot have constructor A call constructor B and constructor B call constructor A, as this would create an infinite loop.