Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is constructor chaining in C#?
Constructor chaining is a way to call one constructor from another constructor in the same class to reuse code and avoid duplication.
Click to reveal answer
beginner
How do you call one constructor from another in C#?
You use the this keyword followed by parentheses with arguments inside the constructor definition.
Click to reveal answer
beginner
Why is constructor chaining useful?
It helps avoid repeating code by letting constructors share common initialization logic, making code cleaner and easier to maintain.
Click to reveal answer
intermediate
What happens if you don't use constructor chaining and repeat code?
You risk having inconsistent initialization and more bugs because changes must be made in multiple places.
Click to reveal answer
beginner
Show a simple example of constructor chaining in C#.
Example:<br><pre>class Car {
public string Model;
public int Year;
public Car() : this("Unknown", 0) {}
public Car(string model, int year) {
Model = model;
Year = year;
}
}</pre>
Click to reveal answer
Which keyword is used for constructor chaining in C#?
Athis
Bbase
Csuper
Dself
✗ Incorrect
The this keyword calls another constructor in the same class.
What is the main benefit of constructor chaining?
AIncrease program speed
BAvoid code duplication
CCreate multiple objects
DChange class inheritance
✗ Incorrect
Constructor chaining helps reuse code and avoid repeating the same initialization.
In constructor chaining, where must the this call appear?
AAs the first statement in the constructor
BAnywhere in the constructor
CAt the end of the constructor
DOutside the constructor
✗ Incorrect
The this call must be the first statement inside the constructor.
What happens if you chain constructors incorrectly?
AProgram ignores chaining
BProgram runs slower
CCompilation error
DCreates infinite loop
✗ Incorrect
Incorrect chaining causes a compilation error because the syntax rules are strict.
Can constructor chaining call a constructor from a base class?
ANo, constructors cannot call other constructors
BYes, use <code>this</code> keyword
CYes, use <code>super</code> keyword
DNo, use <code>base</code> keyword for that
✗ Incorrect
To call a base class constructor, use the base keyword, not this.
Explain how constructor chaining works in C# and why it is useful.
Think about how constructors can share code inside the same class.
You got /4 concepts.
Write a simple C# class with two constructors where one constructor calls the other using constructor chaining.
Remember the this keyword must be the first statement in the constructor.
You got /4 concepts.
Practice
(1/5)
1. What does constructor chaining in C# allow you to do?
easy
A. Override a constructor in a derived class
B. Call a method from a constructor
C. Create multiple instances of a class at once
D. Call one constructor from another constructor in the same class
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 D
Quick Check:
Constructor chaining = calling another constructor [OK]
Hint: Constructor chaining calls another constructor in the same class [OK]
Common Mistakes:
Confusing constructor chaining with method calls
Thinking it creates multiple objects
Mixing it up with inheritance or overriding
2. Which of the following is the correct syntax to chain constructors in C#?
easy
A. public MyClass() : base() { }
B. public MyClass() : this() { }
C. public MyClass() : this(5) { }
D. public MyClass() { this(); }
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 C
Quick Check:
Constructor chaining syntax = : this(...) [OK]
Hint: Use ': this(...)' after constructor signature to chain [OK]
Common Mistakes:
Using 'this()' inside constructor body instead of after signature
Confusing base() with this()
Calling the same constructor recursively
3. What will be the output of this C# code?
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();
}
}
medium
A. Constructor with 10
Default constructor
B. Constructor with 10
C. Default constructor
Constructor with 10
D. Default constructor
Solution
Step 1: Understand constructor chaining call order
The parameterless constructor calls this(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 A
Quick Check:
Chained constructor runs first, then caller prints [OK]
Hint: Chained constructor runs before the calling constructor body [OK]
Common Mistakes:
Assuming calling constructor runs first
Ignoring constructor chaining order
Mixing output order
4. Identify the error in this constructor chaining code:
class Sample {
public Sample() : this() {
Console.WriteLine("Hello");
}
}
medium
A. Missing base keyword for chaining
B. Recursive constructor call causing infinite loop
C. Constructor chaining syntax is correct
D. Constructor must have a return type
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 B