Bird
0
0

Find the error in this constructor chaining code: class Example { public Example(int x) { Console.WriteLine(x); } public Example() : this() { Console.WriteLine("Default"); } }

medium📝 Debug Q7 of 15
C Sharp (C#) - Inheritance
Find the error in this constructor chaining code: class Example { public Example(int x) { Console.WriteLine(x); } public Example() : this() { Console.WriteLine("Default"); } }
ANo error, code is valid
BConstructor chaining calls itself causing infinite recursion
CCannot chain constructors with different parameters
DMissing parameter in chained constructor call
Step-by-Step Solution
Solution:
  1. Step 1: Check chaining target

    The parameterless constructor calls itself with ': this()', causing infinite recursion.
  2. Step 2: Confirm error type

    This results in a compile-time error due to recursive constructor call.
  3. Final Answer:

    Constructor chaining calls itself causing infinite recursion -> Option B
  4. Quick Check:

    Constructor cannot chain to itself directly [OK]
Quick Trick: Do not chain a constructor to itself to avoid recursion [OK]
Common Mistakes:
MISTAKES
  • Assuming chaining to same constructor is allowed
  • Ignoring parameter mismatch
  • Thinking chaining requires same parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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