Bird
0
0

Which of the following is the correct syntax to chain constructors in C#?

easy📝 Syntax Q12 of 15
C Sharp (C#) - Inheritance
Which of the following is the correct syntax to chain constructors in C#?
Apublic MyClass() : base() { }
Bpublic MyClass() : this() { }
Cpublic MyClass() : this(5) { }
Dpublic MyClass() { this(); }
Step-by-Step Solution
Solution:
  1. Step 1: Recall constructor chaining syntax

    Constructor chaining uses : this(parameters) after the constructor signature to call another constructor in the same class.
  2. 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.
  3. Final Answer:

    public MyClass() : this(5) { } -> Option C
  4. Quick Check:

    Constructor chaining syntax = : this(...) [OK]
Quick Trick: Use ': this(...)' after constructor signature to chain [OK]
Common Mistakes:
MISTAKES
  • Using 'this()' inside constructor body instead of after signature
  • Confusing base() with this()
  • Calling the same constructor recursively

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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