Bird
0
0

What will be the output of the following C# code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Interfaces

What will be the output of the following C# code?

interface IA { void Show(); }
interface IB { void Show(); }
class Demo : IA, IB {
  void IA.Show() { Console.WriteLine("IA Show"); }
  void IB.Show() { Console.WriteLine("IB Show"); }
}
class Program {
  static void Main() {
    Demo d = new Demo();
    ((IA)d).Show();
    ((IB)d).Show();
  }
}
AIA Show\nIA Show
BIA Show\nIB Show
CIB Show\nIB Show
DCompilation error due to Show method
Step-by-Step Solution
Solution:
  1. Step 1: Understand explicit interface implementation

    The class Demo implements Show() explicitly for IA and IB, so calls must be cast to interface type.
  2. Step 2: Analyze Main method calls

    Calling ((IA)d).Show() invokes IA.Show(), printing "IA Show"; similarly, ((IB)d).Show() prints "IB Show".
  3. Final Answer:

    IA Show\nIB Show -> Option B
  4. Quick Check:

    Explicit interface calls = separate outputs [OK]
Quick Trick: Explicit interface methods require casting to call [OK]
Common Mistakes:
MISTAKES
  • Expecting one Show() to handle both interfaces
  • Ignoring explicit implementation syntax
  • Assuming compilation error due to method name clash

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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