Forgot to commit
[mono.git] / mcs / docs / ecma334 / 20.4.2.xml
1 <?xml version="1.0"?>
2 <clause number="20.4.2" title="Interface mapping">
3   <paragraph>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. </paragraph>
4   <paragraph>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: <list><list_item> 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. </list_item><list_item> Otherwise, if S contains a declaration of a non-static public member that matches M, then this member is the implementation of I.M. </list_item></list></paragraph>
5   <paragraph>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. </paragraph>
6   <paragraph>For purposes of interface mapping, a class member A matches an interface member B when: <list><list_item> A and B are methods, and the name, type, and formal parameter lists of A and B are identical. </list_item><list_item> 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). </list_item><list_item> A and B are events, and the name and type of A and B are identical. </list_item><list_item> 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). </list_item></list></paragraph>
7   <paragraph>Notable implications of the interface-mapping algorithm are: <list><list_item> 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. </list_item><list_item> Neither non-public nor static members participate in interface mapping. </list_item></list></paragraph>
8   <paragraph>
9     <example>[Example: In the example <code_example><![CDATA[
10 interface ICloneable  
11 {  
12    object Clone();  
13 }  
14 class C: ICloneable  
15 {  
16    object ICloneable.Clone() {...}  
17    public object Clone() {...}  
18 }  
19 ]]></code_example>the ICloneable.Clone member of C becomes the implementation of Clone in ICloneable because explicit interface member implementations take precedence over other members. end example]</example>
20   </paragraph>
21   <paragraph>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. </paragraph>
22   <paragraph>
23     <example>[Example: For example <code_example><![CDATA[
24 interface IControl  
25 {  
26    void Paint();  
27 }  
28 interface IForm  
29 {  
30    void Paint();  
31 }  
32 class Page: IControl, IForm  
33 {  
34    public void Paint() {...}  
35 }  
36 ]]></code_example></example>
37   </paragraph>
38   <paragraph>
39     <example>Here, the Paint methods of both IControl and IForm are mapped onto the Paint method in Page. It is of course also possible to have separate explicit interface member implementations for the two methods. end example]</example>
40   </paragraph>
41   <paragraph>If a class or struct implements an interface that contains hidden members, then some members must necessarily be implemented through explicit interface member implementations. <example>[Example: For example <code_example><![CDATA[
42 interface IBase  
43 {  
44    int P { get; }  
45 }  
46 interface IDerived: IBase  
47 {  
48    new int P();  
49 }  
50 ]]></code_example></example></paragraph>
51   <paragraph>
52     <example>An implementation of this interface would require at least one explicit interface member implementation, and would take one of the following forms <code_example><![CDATA[
53 class C: IDerived  
54 {  
55    int IBase.P { get {...} }  
56    int IDerived.P() {...}  
57 }  
58 class C: IDerived  
59 {  
60    public int P { get {...} }  
61    int IDerived.P() {...}  
62 }  
63 class C: IDerived  
64 {  
65    int IBase.P { get {...} }  
66    public int P() {...}  
67 }  
68 ]]></code_example>end example]</example>
69   </paragraph>
70   <paragraph>When a class implements multiple interfaces that have the same base interface, there can be only one implementation of the base interface. <example>[Example: In the example <code_example><![CDATA[
71 interface IControl  
72 {  
73    void Paint();  
74 }  
75 interface ITextBox: IControl  
76 {  
77    void SetText(string text);  
78 }  
79 interface IListBox: IControl  
80 {  
81    void SetItems(string[] items);  
82 }  
83 class ComboBox: IControl, ITextBox, IListBox  
84 {  
85    void IControl.Paint() {...}  
86    void ITextBox.SetText(string text) {...}  
87    void IListBox.SetItems(string[] items) {...}  
88 }  
89 ]]></code_example>it is not possible to have separate implementations for the IControl named in the base class list, the IControl inherited by ITextBox, and the IControl inherited by IListBox. Indeed, there is no notion of a separate identity for these interfaces. Rather, the implementations of ITextBox and IListBox share the same implementation of IControl, and ComboBox is simply considered to implement three interfaces, IControl, ITextBox, and IListBox. end example]</example> </paragraph>
90   <paragraph>The members of a base class participate in interface mapping. <example>[Example: In the example <code_example><![CDATA[
91 interface Interface1  
92 {  
93    void F();  
94 }  
95 class Class1  
96 {  
97    public void F() {}  
98    public void G() {}  
99 }  
100 class Class2: Class1, Interface1  
101 {  
102    new public void G() {}  
103 }  
104 ]]></code_example>the method F in Class1 is used in Class2's implementation of Interface1. end example]</example> </paragraph>
105 </clause>