Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (24 sloc) 2.5 KB
title ms.date helpviewer_keywords ms.assetid
Explicit Interface Implementation - C# Programming Guide
07/20/2015
explicit interfaces [C#]
interfaces [C#], explicit
181c901f-0d4c-4f29-97fc-895079617bf2

Explicit Interface Implementation (C# Programming Guide)

If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation. In the following example, all the calls to Paint invoke the same method.

[!code-csharpcsProgGuideInheritance#39]

If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface. This is accomplished by naming the class member with the name of the interface and a period. For example:

[!code-csharpcsProgGuideInheritance#40]

The class member IControl.Paint is only available through the IControl interface, and ISurface.Paint is only available through ISurface. Both method implementations are separate, and neither is available directly on the class. For example:

[!code-csharpcsProgGuideInheritance#41]

Explicit implementation is also used to resolve cases where two interfaces each declare different members of the same name such as a property and a method:

[!code-csharpcsProgGuideInheritance#42]

To implement both interfaces, a class has to use explicit implementation either for the property P, or the method P, or both, to avoid a compiler error. For example:

[!code-csharpcsProgGuideInheritance#43]

See also

You can’t perform that action at this time.