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#?
✗ Incorrect
The
this keyword calls another constructor in the same class.What is the main benefit of constructor chaining?
✗ Incorrect
Constructor chaining helps reuse code and avoid repeating the same initialization.
In constructor chaining, where must the
this call appear?✗ Incorrect
The
this call must be the first statement inside the constructor.What happens if you chain constructors incorrectly?
✗ Incorrect
Incorrect chaining causes a compilation error because the syntax rules are strict.
Can constructor chaining call a constructor from a base class?
✗ 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.