skip to content

In C#, what is the difference between implementing an interface member implicitly and explicitly, and how can callers reach each one?

level: juniorimportance: must knowfreq 68%

answer

  1. public member vs qualified name
  2. the caller's static type decides
  3. CS1061 on the class variable
  4. no access modifier allowed

basics

~20 s

An implicit implementation is a public member matching the interface member, callable through the class or the interface. An explicit one, named like IPaymentGateway.Close with no access modifier, is callable only through an interface-typed reference.

solid answer

~40 s

With implicit implementation the class declares an ordinary `public void Charge(decimal amount)` and the compiler maps it to `IPaymentGateway.Charge` by name and signature, so `adapter.Charge(...)` and `gateway.Charge(...)` call the same method. With explicit implementation the member is declared as `void IPaymentGateway.Close()`: it has no access modifier (adding `public` or `private` is CS0106), it is not part of the class's own surface, and `adapter.Close()` on a `CardGatewayAdapter` variable fails with CS1061. You reach it by converting to the interface, `((IPaymentGateway)adapter).Close()`, and code inside the class has to cast `this` the same way. Explicit implementation is used to keep a member off the class's public surface, to give two interfaces with the same member name different bodies, or to expose a better-typed public member while still satisfying the interface.

code

csharp · 24 lines
csharp
#:property PublishAot=false
var adapter = new CardGatewayAdapter();
adapter.Charge(25m);                         // implicit: reachable on the class type
// adapter.Close();                          // error CS1061: no member named Close on CardGatewayAdapter

IPaymentGateway gateway = adapter;
gateway.Charge(25m);                         // same public method through the interface
gateway.Close();                             // explicit: reachable only through the interface
adapter.Shutdown();

public interface IPaymentGateway
{
    void Charge(decimal amount);
    void Close();
}

public class CardGatewayAdapter : IPaymentGateway
{
    public void Charge(decimal amount) => Console.WriteLine($"Charge {amount}");

    void IPaymentGateway.Close() => Console.WriteLine("IPaymentGateway.Close");   // no access modifier allowed

    public void Shutdown() => ((IPaymentGateway)this).Close();   // inside the class, cast this
}

go deeper

for a junior

Recall the two shapes: a public method with the same name, or a method named IPaymentGateway.Close with no modifier that only an interface reference can call.

for a middle

Explain the compiler rules behind explicit members: CS0106 for any modifier, CS1061 through the class type, and casting this inside the class.

for a senior

Show when explicit implementation earns its place: same-named members from two interfaces, a stronger-typed public member like List<T>'s struct enumerator, and a protected virtual hook for subclasses.

for a principal

Treat explicit implementation as an API-surface decision: decide which interface members belong on the concrete type's public surface and which are plumbing for one consumer.

## Two ways to satisfy an interface member When a C# class lists an interface in its base list, the compiler must find an implementation for every interface member that has no default body. It looks for one in two forms: - **Implicit implementation**: a `public` instance member of the class (or of a base class) with the same name and signature. - **Explicit implementation**: a member whose name is qualified with the interface, such as `void IPaymentGateway.Close()`. Both satisfy the interface. The difference is **who can call the member, and through which static type**. ## Implicit implementation An implicit implementation is an ordinary public member that also happens to match the interface: ```csharp public class CardGatewayAdapter : IPaymentGateway { public void Charge(decimal amount) { /* ... */ } } ``` The method belongs to the class. A caller holding a `CardGatewayAdapter` variable calls it directly, and a caller holding an `IPaymentGateway` reference reaches the same method through interface dispatch. It must be `public`; a non-public method with the right signature does not implement the member (CS0737). It may also be `virtual`, so a subclass can override it and interface callers see the override. ## Explicit implementation An explicit implementation exists only as the interface's member: ```csharp void IPaymentGateway.Close() { /* ... */ } ``` The compiler enforces several rules: 1. **No access or inheritance modifiers.** `public`, `private`, `virtual` or `abstract` on it is CS0106, "The modifier 'public' is not valid for this item". 2. **Not on the class's surface.** `adapter.Close()` on a `CardGatewayAdapter` variable is CS1061: the class "does not contain a definition for 'Close'". 3. **Not by simple name inside the class either.** The class's own code calls it as `((IPaymentGateway)this).Close()`. 4. **The qualifier names the declaring interface.** If `Close` is declared on a base interface, the declaration must name that interface; naming a derived interface is CS0539. | | Implicit | Explicit | |---|---|---| | Declaration | `public void Close()` | `void IPaymentGateway.Close()` | | Access modifier | `public` required | none allowed (CS0106) | | Call through the class type | yes | no (CS1061) | | Call through the interface | yes | yes | | Can be `virtual` | yes | no | ## What the compiler emits The difference is visible in the compiled metadata, which reflection shows directly: - The implicit `Charge` is emitted as a **public** method marked `virtual` and `final` (sealed) when the source did not say `virtual`. The runtime needs a virtual slot for interface dispatch, and `final` keeps subclasses from overriding a method the source never declared virtual. - The explicit `Close` is emitted as a **private** `virtual final` method whose metadata name is literally `IPaymentGateway.Close`, plus an entry that records it as the implementation of the interface method. - `Type.GetInterfaceMap(typeof(IPaymentGateway))` returns the result: `Charge -> Charge` and `Close -> IPaymentGateway.Close`. That private, qualified name matches the language rule: member lookup on the class never finds an explicit implementation, so the class's own code cannot call it by simple name either. ## Why a team chooses explicit implementation - **Keep a member off the class's surface.** A payment adapter may need `Close` only because a hosting component disposes gateways through the interface; application code should not call it by accident. - **Give two interfaces different bodies** when both declare a member with the same name, or when one declares a property and the other a method of the same name. - **Expose a better-typed public member.** The base class library does this: `List<T>` implements `IEnumerable<T>.GetEnumerator()` and `IEnumerable.GetEnumerator()` explicitly, while its public `GetEnumerator()` returns the `List<T>.Enumerator` struct. ## Traps around explicit members - A subclass cannot override an explicit implementation, because it cannot be `virtual`. The usual design is an explicit member that calls a `protected virtual` method, or a subclass that lists the interface again and re-implements it. - Calling an explicit member on a struct variable means converting it to the interface, which boxes it; a generic method constrained to the interface can call it without boxing, and the costs are covered with boxing. - An explicit implementation is still public **through the interface**: hiding a member this way is about API shape, not security.

  • How does code inside the class call its own explicitly implemented member?
    It converts `this` to the interface: `((IPaymentGateway)this).Close()`. Writing `Close()` or `this.Close()` fails, because the explicit implementation is not a member the class exposes by name; the compiler reports CS0103 or CS1061 unless some other member named `Close` exists.
  • Can a derived class override an explicit interface implementation?
    No. An explicit implementation cannot be `virtual` (CS0106), so there is nothing to override. Either have the explicit member call a `protected virtual` method that subclasses override, or let the subclass list the interface again and supply its own explicit implementation, which re-implements the interface for that subclass.

  • An explicit implementation is private, so the class calls it by name.
  • An explicit implementation needs public to be reachable through the interface.
  • Implementing a member explicitly removes it from the interface contract.
  • An implicit implementation must be marked override to satisfy the interface.
  • A subclass can override an explicit implementation like a virtual method.