13 / 13

Explain "Explicit Interface Implementation" (C#) and when it's needed.

Difficulty: 5/10
interface member collisions, access control, versioning

Explicit Interface Implementation in C#

Explicit interface implementation in C# allows a class to implement an interface member so that the member is accessible through the interface reference rather than directly through the class instance. It is particularly useful when two interfaces define members with the same signature but require different behavior, or when an interface member should not form part of the class's normal public API. This provides precise control over interface-specific behavior and avoids naming conflicts.

javascript
  1. 1

    Used when multiple interfaces contain conflicting member signatures.

  2. 2

    Allows different implementations for the same interface member name.

  3. 3

    The explicitly implemented member is normally accessed through the interface reference.

  4. 4

    Keeps interface-specific behavior out of the class's ordinary public surface.

  5. 5

    Useful for API compatibility when implementing multiple contracts.

  6. 6

    Should be used intentionally because it can make members less discoverable to callers holding the concrete type.

Scenario Questions

0-2 years experience

  1. 1We have two interfaces, IFoo with void DoWork() and IBar with void DoWork(). You need a class that implements both without exposing two DoWork methods publicly. How would you write that in C#?
  2. 2If you call ((IFoo)myObj).DoWork() on an object that uses explicit interface implementation, what happens if you forget the cast and just call myObj.DoWork()?

2-5 years experience

  1. 1You added a new method to an existing interface that many classes implement explicitly. After deployment, a runtime error occurs when older code calls the method via the interface. Walk me through why that happened and how you'd fix it.
  2. 2During a code review, a teammate suggests removing explicit interface implementations to simplify the class. What trade‑offs would you discuss, especially regarding name collisions and API surface?

5-8 years experience

  1. 1Our product library exposes several interfaces that share member names, and we need to keep backward compatibility while adding new overloads. How would you design the implementation strategy using explicit interface implementation, and what impact does it have on assembly versioning and reflection?
  2. 2We have a performance‑critical component that uses reflection to discover interface members at runtime. How does explicit interface implementation affect the reflection code, and what would you do to ensure correct behavior without a performance hit?

8+ years experience

  1. 1We're consolidating multiple micro‑services that each expose similar contracts into a shared SDK. Some contracts have overlapping method names. How would you use explicit interface implementation across the SDK to avoid breaking existing consumers, and what governance processes would you put in place?
  2. 2A legacy system uses COM interop and expects certain interface methods to be callable directly. Introducing explicit interface implementations could change the v‑table layout. How would you evaluate the risk and plan a migration strategy?

Follow-up Questions

  • Can you write a short code snippet that demonstrates the syntax?
  • What would happen if you tried to call the method without casting to the interface?
  • How does this affect unit tests that mock the interface?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.