Bird
0
0

What is the issue with this code?

medium📝 Debug Q6 of 15
C Sharp (C#) - Interfaces
What is the issue with this code?
interface ITest { void Run() => Console.WriteLine("Start"); } class Test : ITest { public void Run() { Console.WriteLine("End"); } }
AThe class method must be explicitly marked 'override' to override the interface default method.
BThe interface method cannot have a body; default methods are not allowed.
CThe class method must have the same accessibility as the interface method and be public.
DThe interface method must be static to have a body.
Step-by-Step Solution
Solution:
  1. Step 1: Check interface method declaration

    The interface method 'Run' is a default method with a body, which is valid in C# 8.0+.
  2. Step 2: Check class implementation

    The class 'Test' implements 'Run' but the method is declared without an accessibility modifier, so it defaults to private.
  3. Step 3: Understand interface implementation rules

    Interface methods must be implemented with public accessibility in the class. Otherwise, the class does not properly implement the interface method.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Interface implementations must be public in classes [OK]
Quick Trick: Interface methods must be public in implementing classes [OK]
Common Mistakes:
MISTAKES
  • Assuming 'override' keyword is needed for interface methods
  • Thinking interface methods cannot have bodies
  • Forgetting to make class method public

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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