Bird
Raised Fist0

What is wrong with this code snippet?

medium📝 Debug Q7 of Q15
C Sharp (C#) - Inheritance
What is wrong with this code snippet?
class Parent {
    public Parent(int x) { }
}
class Child : Parent {
    public Child() {
        base();
    }
}
AParent constructor must be parameterless
Bbase() cannot be called like a method inside constructor body
CChild constructor must have int parameter
DNo error, code is valid
Step-by-Step Solution
Solution:
  1. Step 1: Understand base constructor call syntax

    In C#, calling a base class constructor must be done in the constructor initializer, not inside the constructor body.
  2. Step 2: Identify the syntax error

    The code incorrectly calls base(); inside the constructor body, which is invalid syntax.
  3. Final Answer:

    base() cannot be called like a method inside constructor body -> Option B
  4. Quick Check:

    base constructor call must be in initializer [OK]
Quick Trick: Call base constructor after colon, not inside constructor body [OK]
Common Mistakes:
MISTAKES
  • Calling base() inside constructor body
  • Not matching base constructor parameters
  • Assuming base() is a method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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