C# Interview Question #24
What is constructor chaining in C#?
Classes, Constructors & Properties Junior Beginner
Quick Interview Answer
Constructor chaining means one constructor calls another constructor. The this keyword calls another constructor in the same class, while base calls a constructor in the base class.
Detailed Explanation
Constructor chaining means one constructor calls another constructor. The this keyword calls another constructor in the same class, while base calls a constructor in the base class.
Constructor chaining reduces duplicated initialization logic and keeps object construction consistent.
Code Example
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
public Product() : this("Unknown", 0) { }
public Product(string name) : this(name, 0) { }
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
}