
20 Interfaces
20.4 Interface implementations
20.4.1 Explicit interface member implementations
Paragraph 11 For purposes of implementing interfaces, a class or struct may declare explicit interface member implementations. 2 An explicit interface member implementation is a method, property, event, or indexer declaration that references a fully qualified interface member name. [Example: 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. end example] [Example: In some cases, the name of an interface member may not be appropriate for the implementing class, in which case the interface member may be implemented using explicit interface member implementation. A class implementing a file abstraction, for example, would likely implement a Close member function that has the effect of releasing the file resource, and implement the Dispose method of the IDisposable interface using explicit interface member implementation:
interface IDisposable {
void Dispose();
}
class MyFile: IDisposable {
void IDisposable.Dispose() {
Close();
}
public void Close() {
// Do what's necessary to close the file
System.GC.SuppressFinalize(this);
}
}
end example]
Paragraph 21 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. 2 An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.
Paragraph 31 It is a compile-time error for an explicit interface member implementation to include access modifiers, and it is a compile-time error to include the modifiers abstract, virtual, override, or static.
Paragraph 41 Explicit interface member implementations have different accessibility characteristics than other members. 2 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. 3 However, since they can be accessed through an interface instance, they are in a sense also public.
Paragraph 51 Explicit interface member implementations serve two primary purposes:
class Shape: ICloneable
{
object ICloneable.Clone() {...}
int IComparable.CompareTo(object other) {...} // invalid
}
the declaration of IComparable.CompareTo results in a compile-time error 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() {...} // invalid
}
the declaration of ICloneable.Clone in Ellipse results in a compile-time error because ICloneable is not explicitly listed in the base class list of Ellipse. end example]
Paragraph 71 The fully qualified name of an interface member must reference the interface in which the member was declared. [Example: 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. end example]
| |
| Jagger Software Ltd | |
| Company # 4070126 | |
| VAT # 762 5213 42 |