X hits on this document

1049 views

0 shares

0 downloads

0 comments

294 / 396

Chapter ‎18   Unsafe code

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.

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); } }

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 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.

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:

Because explicit interface member implementations are not accessible through class or struct instances, they allow interface implementations to be excluded from the public interface of a class or struct. This is particularly useful when a class or struct implements an internal interface that is of no interest to a consumer of that class or struct.

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.281

Document info
Document views1049
Page views1049
Page last viewedSat Dec 10 13:44:44 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments