C# Reference ยท Classes and Object-Oriented Concepts
Explicit interface implementation in C#
Reference for C# explicit interface implementation: resolve name clashes, hide members from the class API, implement the same interface twice and call members via a cast.
Normally a class implements an interface member with a public member of the same name. With explicit interface implementation you write the interface name in front of the member (void IShape.Draw()), and the member becomes reachable only through a variable of that interface type. It has no access modifier and does not appear on the class itself.
Use it to resolve name conflicts between interfaces, to hide members that would clutter the class's public API, or to give different behavior to the same method name depending on the interface used.
class Report : IDisposable { void IDisposable.Dispose() { /* only reachable through IDisposable */ } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
class C : IA, IB { void IA.Method() { } // no access modifier int IA.Prop { get; set; } event EventHandler IB.Changed { add { } remove { } } string IB.this[int i] => ""; }
| Rule | Description |
|---|---|
| Name | Prefixed with the interface name, including its generic arguments (ICollection<string>.IsReadOnly). |
| Modifiers | None are allowed (not even public); the member is effectively private to the class but callable through the interface. |
| Access | Only through an expression of the interface type: a cast, an interface-typed variable or parameter, or a generic constrained to the interface. |
| Inheritance | Cannot be virtual or override; derived classes cannot call it with base. |
Errors: CS1061 when you call the member on the class type; CS0539 if the interface has no such member.
Usage notes
- Name clashes. Two interfaces with the same method can get different implementations, as in the first example. If both should do the same thing, one public member satisfies both.
- Hiding noise. BCL collections use it to keep rarely used members off the public surface; for instance
Dictionary<K,V>implements manyICollectionmembers explicitly. - Struct pitfalls. Calling an explicit implementation on a struct through the interface boxes the value.
- Not extendable. A derived class cannot call or override the explicit member. If subclasses need to customize it, expose a
protected virtualmethod and have the explicit member call it. - Discoverability. IntelliSense does not show the member on the class, which can surprise users. Document the interface relationship.
- Generic math and static abstract members also use explicit syntax (
static T IFoo<T>.Create()) when two interfaces compete for a name. - Default interface members can be re-implemented by an interface with the same syntax; see default interface members.