For purposes of implementing interfaces, a class or struct may declare explicit interface member implementations. An explicit interface member implementation is a method, property, event, or indexer declaration that references a fully qualified interface member name. For example
interface ICloneable { object Clone(); } interface IComparable { int CompareTo(object other); } class ListEntry: ICloneable, IComparable { object ICloneable.Clone() {...} int IComparable.CompareTo(object other) {...} }
Here, ICloneable.Clone
and IComparable.CompareTo
are explicit interface member implementations.
It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.
It is an error for an explicit interface member implementation to include access modifiers, as is it an error to include the abstract
, virtual
, override
, or static
modifiers.
Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through their fully qualified name in a method invocation or a property access, they are in a sense private. However, since they can be accessed through an interface instance, they are in a sense also public.
Explicit interface member implementations serve two primary purposes:
For an explicit interface member implementation to be valid, the class or struct must name an interface in its base class list that contains a member whose fully qualified name, type, and parameter types exactly match those of the explicit interface member implementation. Thus, in the following class
class Shape: ICloneable { object ICloneable.Clone() {...} int IComparable.CompareTo(object other) {...} }
the declaration of IComparable.CompareTo
is invalid because IComparable
is not listed in the base class list of Shape
and is not a base interface of ICloneable
. Likewise, in the declarations
class Shape: ICloneable { object ICloneable.Clone() {...} } class Ellipse: Shape { object ICloneable.Clone() {...} }
the declaration of ICloneable.Clone
in Ellipse
is in error because ICloneable
is not explicitly listed in the base class list of Ellipse
.
The fully qualified name of an interface member must reference the interface in which the member was declared. Thus, in the declarations
interface IControl { void Paint(); } interface ITextBox: IControl { void SetText(string text); } class TextBox: ITextBox { void IControl.Paint() {...} void ITextBox.SetText(string text) {...} }
the explicit interface member implementation of Paint
must be written as IControl.Paint
.
For further information, see http://comrtime/specs/Formats/MiniSpec%20MethodImpls.doc