X hits on this document

778 views

0 shares

0 downloads

0 comments

296 / 396

Chapter ‎18   Unsafe code

Interface mapping for a class or struct C locates an implementation for each member of each interface specified in the base class list of C. The implementation of a particular interface member I.M, where I is the interface in which the member M is declared, is determined by examining each class or struct S, starting with C and repeating for each successive base class of C, until a match is located:

If S contains a declaration of an explicit interface member implementation that matches I and M, then this member is the implementation of I.M.

Otherwise, if S contains a declaration of a non-static public member that matches M, then this member is the implementation of I.M.

A compile-time error occurs if implementations cannot be located for all members of all interfaces specified in the base class list of C. Note that the members of an interface include those members that are inherited from base interfaces.

For purposes of interface mapping, a class member A matches an interface member B when:

A and B are methods, and the name, type, and formal parameter lists of A and B are identical.

A and B are properties, the name and type of A and B are identical, and A has the same accessors as B (A is permitted to have additional accessors if it is not an explicit interface member implementation).

A and B are events, and the name and type of A and B are identical.

A and B are indexers, the type and formal parameter lists of A and B are identical, and A has the same accessors as B (A is permitted to have additional accessors if it is not an explicit interface member implementation).

Notable implications of the interface mapping algorithm are:

Explicit interface member implementations take precedence over other members in the same class or struct when determining the class or struct member that implements an interface member.

Neither non-public nor static members participate in interface mapping.

In the example

interface ICloneable { object Clone(); }

class C: ICloneable { object ICloneable.Clone() {...}

public object Clone() {...} }

the ICloneable.Clone member of C becomes the implementation of Clone in ICloneable because explicit interface member implementations take precedence over other members.

If a class or struct implements two or more interfaces containing a member with the same name, type, and parameter types, it is possible to map each of those interface members onto a single class or struct member. For example

interface IControl { void Paint(); }

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

Document info
Document views778
Page views778
Page last viewedSat Dec 03 11:42:14 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments