13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 20 InterfacesHere, class TextBox implements both IControl and ITextBox. end example]20.4.1 Explicit interface member implementationsFor purposes of implementing interfaces, a class or struct may declare explicit interface memberimplementations. An explicit interface member implementation is a method, property, event, or indexerdeclaration that references a fully qualified interface member name. [Example: For exampleinterface 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. endexample][Example: In some cases, the name of an interface member may not be appropriate for the implementing class, inwhich case the interface member may be implemented using explicit interface member implementation. A classimplementing a file abstraction, for example, would likely implement a Close member function that has theeffect of releasing the file resource, and implement the Dispose method of the IDisposable interface usingexplicit interface member implementation:interface IDisposable {void Dispose();}class MyFile: IDisposable{void IDisposable.Dispose() {Close();}public void Close() {// Do what's necessary to close the fileSystem.GC.SuppressFinalize(this);}}end example]It is not possible to access an explicit interface member implementation through its fully qualified name in amethod invocation, property access, or indexer access. An explicit interface member implementation can only beaccessed 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 acompile-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 amethod invocation or a property access, they are in a sense private. However, since they can be accessed throughan 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, theyallow interface implementations to be excluded from the public interface of a class or struct. This is285

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!