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
How constructor chaining works
📖 Scenario: Imagine you are creating a simple program to represent a Car. Different cars can have different details like brand, model, and year. You want to use constructor chaining to make your code cleaner and avoid repeating yourself.
🎯 Goal: You will build a Car class with multiple constructors that use constructor chaining. This will help you understand how one constructor can call another to reuse code.
📋 What You'll Learn
Create a class called Car with three fields: brand, model, and year.
Write a constructor that takes only brand and sets default values for model and year.
Write a constructor that takes brand and model, and calls the first constructor using constructor chaining.
Write a constructor that takes brand, model, and year, and calls the second constructor using constructor chaining.
Create an object of Car using the constructor with all three parameters.
Print the car details to show the values of brand, model, and year.
💡 Why This Matters
🌍 Real World
Constructor chaining is used in real programs to simplify object creation when there are many ways to create an object with different details.
💼 Career
Understanding constructor chaining is important for writing clean, maintainable code in object-oriented programming jobs.
Progress0 / 4 steps
1
Create the Car class with fields and first constructor
Create a class called Car with three fields: brand, model, and year. Then write a constructor that takes a string parameter brand and sets brand to this value, model to "Unknown", and year to 0.
C Sharp (C#)
Hint
Remember to declare the fields inside the class and write a constructor with one parameter brand. Set the other fields to default values.
2
Add second constructor with constructor chaining
Add a second constructor to the Car class that takes two string parameters: brand and model. Use constructor chaining to call the first constructor with brand, then set model to the given value.
C Sharp (C#)
Hint
Use : this(brand) after the constructor signature to call the first constructor.
3
Add third constructor with full parameters and chaining
Add a third constructor to the Car class that takes three parameters: brand, model, and year. Use constructor chaining to call the second constructor with brand and model, then set year to the given value.
C Sharp (C#)
Hint
Use : this(brand, model) to call the second constructor and then set year.
4
Create a Car object and print its details
Create a Car object called myCar using the constructor with three parameters: "Toyota", "Corolla", and 2020. Then print the values of myCar.brand, myCar.model, and myCar.year separated by spaces.
C Sharp (C#)
Hint
Create the object with new Car("Toyota", "Corolla", 2020) and print the fields using Console.WriteLine with string interpolation.
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