Bird
Raised Fist0

Identify the problem in this constructor chaining code:

medium📝 Debug Q6 of Q15
C Sharp (C#) - Inheritance
Identify the problem in this constructor chaining code:
class Gadget {
  public Gadget() : this() {
    Console.WriteLine("Hello Gadget");
  }
}
AConstructor chaining syntax is correct
BMissing base keyword for chaining
CConstructor calls itself recursively causing infinite loop
DConstructor cannot have parameters
Step-by-Step Solution
Solution:
  1. Step 1: Examine chaining call

    The constructor calls itself using : this(), which leads to infinite recursion.
  2. Step 2: Identify error

    This causes a compile-time error or stack overflow at runtime.
  3. Final Answer:

    Constructor calls itself recursively causing infinite loop -> Option C
  4. Quick Check:

    Constructor cannot chain to itself [OK]
Quick Trick: Avoid chaining constructor to itself [OK]
Common Mistakes:
MISTAKES
  • Using 'this()' to call same constructor
  • Confusing 'base' and 'this' keywords
  • Assuming chaining syntax is always valid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes