Bird
0
0

Find the error in this C# code:

medium📝 Debug Q7 of 15
C Sharp (C#) - Inheritance
Find the error in this C# code:
class Base {
  public virtual void Show() {}
}
class Derived : Base {
  public void Show() {}
}
ABase.Show cannot be virtual
BDerived.Show must use override keyword
CDerived.Show must be static
DNo error, code is valid
Step-by-Step Solution
Solution:
  1. Step 1: Understand virtual method overriding rules

    A method overriding a virtual method must use the override keyword in C#.
  2. Step 2: Check Derived class method declaration

    Derived.Show lacks the override keyword, so it hides the base method instead of overriding it, which causes a warning or error.
  3. Final Answer:

    Derived.Show must use override keyword -> Option B
  4. Quick Check:

    Override virtual methods with 'override' keyword [OK]
Quick Trick: Use 'override' to override virtual methods [OK]
Common Mistakes:
MISTAKES
  • Omitting override keyword
  • Confusing hiding with overriding
  • Assuming virtual methods don't require override

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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