Phase 8 of 30 · Topic 8.2

Explicit Interface Implementation & Name Collision Resolution

1Concept

Explicit interface implementation (`void IInterface.Method()`) hides interface methods from the class's public API, preventing naming collisions and forcing access through the interface pointer.

2Architecture Diagram

Class `OrderService` implements `IClientApi` and `IInternalAdminApi`:
├── public void Process() ──> Visible to general consumers
├── void IInternalAdminApi.Purge() ──> Visible ONLY when cast to IInternalAdminApi

3Code Example

C# 13 & .NET 9
using System;

public interface IEnglishGreeting { void Greet(); }
public interface ISpanishGreeting { void Greet(); }

public class MultiLingualHost : IEnglishGreeting, ISpanishGreeting
{
    void IEnglishGreeting.Greet() => Console.WriteLine("Hello, World!");
    void ISpanishGreeting.Greet() => Console.WriteLine("¡Hola, Mundo!");
}

public class ExplicitInterfaceDemo
{
    public static void Main()
    {
        var host = new MultiLingualHost();
        
        // host.Greet(); // COMPILE ERROR: Explicitly implemented
        ((IEnglishGreeting)host).Greet();
        ((ISpanishGreeting)host).Greet();
    }
}

4Expected Output

Hello, World!
¡Hola, Mundo!

5Key Takeaways

  • Explicit interface implementation encapsulates internal contract methods cleanly.
  • Resolves collisions when two interfaces declare identical method signatures.
  • Value type structs with explicit implementations box when cast to the interface.