X hits on this document

1338 views

0 shares

0 downloads

0 comments

295 / 396

C# LANGUAGE SPECIFICATION

Explicit interface member implementations allow disambiguation of interface members with the same signature. Without explicit interface member implementations it would be impossible for a class or struct to have different implementations of interface members with the same signature and return type, as would it be impossible for a class or struct to have any implementation at all of interface members with the same signature but with different return types.

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) {...}// 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.

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.

13.4.2 Interface mapping

A class or struct must provide implementations of all members of the interfaces that are listed in the base class list of the class or struct. The process of locating implementations of interface members in an implementing class or struct is known as interface mapping.

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

Document info
Document views1338
Page views1338
Page last viewedSat Jan 21 21:41:15 UTC 2017
Pages396
Paragraphs9401
Words133190

Comments