Bird
Raised Fist0

You have two interfaces with the same method name:

hard🚀 Application Q8 of Q15
C Sharp (C#) - Interfaces
You have two interfaces with the same method name:
interface IA { void Execute(); }
interface IB { void Execute(); }
class C : IA, IB { ??? }

How do you implement both methods explicitly to avoid conflict?
Avoid IA.Execute() { } void Execute() { }
Bpublic void Execute() { Console.WriteLine("Execute"); }
Cvoid Execute() { Console.WriteLine("Execute"); }
Dvoid IA.Execute() { Console.WriteLine("IA Execute"); } void IB.Execute() { Console.WriteLine("IB Execute"); }
Step-by-Step Solution
Solution:
  1. Step 1: Understand method name conflict

    Both interfaces have Execute method, so explicit implementation is needed to differentiate them.
  2. Step 2: Implement both explicitly

    Implement each interface method by prefixing with interface name to avoid conflict.
  3. Final Answer:

    void IA.Execute() { Console.WriteLine("IA Execute"); } void IB.Execute() { Console.WriteLine("IB Execute"); } -> Option D
  4. Quick Check:

    Explicit implementation resolves method name conflicts [OK]
Quick Trick: Prefix method with interface name to implement multiple same-name methods [OK]
Common Mistakes:
MISTAKES
  • Implementing only one method
  • Using implicit implementation causing conflict
  • Not prefixing interface name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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