Constructor chaining helps you reuse code when creating objects. It lets one constructor call another to avoid repeating setup steps.
How constructor chaining works in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
public class ClassName { public ClassName() : this(default(Type)) { // This constructor calls the other constructor } public ClassName(Type parameter) { // Initialization code here } }
The : this(...) syntax calls another constructor in the same class.
Constructor chaining must be the first statement in the constructor.
public class Person { public string Name; public int Age; public Person() : this("Unknown", 0) { // Calls the constructor with two parameters } public Person(string name, int age) { Name = name; Age = age; } }
public class Box { public int Width; public int Height; public Box() : this(1, 1) { // Default size box } public Box(int size) : this(size, size) { // Square box } public Box(int width, int height) { Width = width; Height = height; } }
public class EmptyExample { public EmptyExample() { } }
This program shows constructor chaining in action. Each constructor calls the next one with more details. The output shows which constructor runs and the final car info.
using System; public class Car { public string Make; public string Model; public int Year; public Car() : this("Unknown", "Unknown", 0) { Console.WriteLine("Default constructor called."); } public Car(string make, string model) : this(make, model, 2024) { Console.WriteLine("Constructor with make and model called."); } public Car(string make, string model, int year) { Make = make; Model = model; Year = year; Console.WriteLine("Constructor with make, model, and year called."); } public void PrintInfo() { Console.WriteLine($"Car: {Make}, Model: {Model}, Year: {Year}"); } } public class Program { public static void Main() { Console.WriteLine("Creating car1 with default constructor:"); Car car1 = new Car(); car1.PrintInfo(); Console.WriteLine(); Console.WriteLine("Creating car2 with make and model constructor:"); Car car2 = new Car("Toyota", "Corolla"); car2.PrintInfo(); Console.WriteLine(); Console.WriteLine("Creating car3 with make, model, and year constructor:"); Car car3 = new Car("Honda", "Civic", 2020); car3.PrintInfo(); } }
Constructor chaining helps reduce repeated code and keeps constructors organized.
Time complexity is the same as normal constructors; it just calls another constructor.
Common mistake: forgetting that chaining must be the first line in the constructor.
Use constructor chaining when you want to provide multiple ways to create an object but share common setup code.
Constructor chaining lets one constructor call another in the same class.
It helps avoid repeating code and keeps initialization consistent.
Use : this(...) syntax as the first line in a constructor to chain.
Practice
Solution
Step 1: Understand constructor chaining concept
Constructor chaining means one constructor calls another constructor within the same class to reuse code.Step 2: Identify what constructor chaining does
It helps avoid repeating initialization code by calling another constructor using: this(...)syntax.Final Answer:
Call one constructor from another constructor in the same class -> Option DQuick Check:
Constructor chaining = calling another constructor [OK]
- Confusing constructor chaining with method calls
- Thinking it creates multiple objects
- Mixing it up with inheritance or overriding
Solution
Step 1: Recall constructor chaining syntax
Constructor chaining uses: this(parameters)after the constructor signature to call another constructor in the same class.Step 2: Analyze options
public MyClass() : this(5) { } uses: this(5)which correctly calls another constructor with an int parameter. public MyClass() : this() { } calls itself recursively causing error. public MyClass() : base() { } calls base class constructor, not chaining. public MyClass() { this(); } tries to call constructor inside body, which is invalid.Final Answer:
public MyClass() : this(5) { } -> Option CQuick Check:
Constructor chaining syntax =: this(...)[OK]
- Using 'this()' inside constructor body instead of after signature
- Confusing base() with this()
- Calling the same constructor recursively
class Test {
public Test() : this(10) {
Console.WriteLine("Default constructor");
}
public Test(int x) {
Console.WriteLine($"Constructor with {x}");
}
}
class Program {
static void Main() {
Test t = new Test();
}
}Solution
Step 1: Understand constructor chaining call order
The parameterless constructor callsthis(10), so the constructor with int parameter runs first.Step 2: Trace output order
First, "Constructor with 10" is printed from the int constructor. Then control returns to parameterless constructor which prints "Default constructor".Final Answer:
Constructor with 10 Default constructor -> Option AQuick Check:
Chained constructor runs first, then caller prints [OK]
- Assuming calling constructor runs first
- Ignoring constructor chaining order
- Mixing output order
class Sample {
public Sample() : this() {
Console.WriteLine("Hello");
}
}Solution
Step 1: Analyze constructor chaining call
The constructor calls itself with: this(), causing infinite recursion.Step 2: Identify error type
This recursive call leads to a runtime stack overflow error because no termination occurs.Final Answer:
Recursive constructor call causing infinite loop -> Option BQuick Check:
Constructor calling itself = infinite recursion [OK]
- Thinking chaining to self is allowed
- Confusing base() and this() calls
- Expecting constructor to have return type
new Box()?class Box {
public int Width, Height;
public Box() : this(5, 10) {
Console.WriteLine("Default Box");
}
public Box(int w, int h) {
Width = w;
Height = h;
Console.WriteLine($"Box: {Width}x{Height}");
}
}Solution
Step 1: Understand constructor chaining and initialization
The parameterless constructor callsthis(5, 10), so the two-parameter constructor runs first, setting Width and Height and printing their values.Step 2: Trace output order
First, "Box: 5x10" is printed from the two-parameter constructor. Then control returns to the parameterless constructor which prints "Default Box".Final Answer:
Box: 5x10 Default Box -> Option AQuick Check:
Chained constructor runs first, then caller prints [OK]
- Assuming default values 0 for Width and Height
- Thinking default constructor runs first
- Ignoring chaining call order
