disable broken tests on net_4_0
[mcs.git] / docs / ecma334 / 8.9.xml
blob08234cbe45b0186656e79f589539a200a389942c
1 <?xml version="1.0"?>
2 <clause number="8.9" title="Interfaces" informative="true">
3   <paragraph>An interface defines a contract. A class or struct that implements an interface must adhere to its contract. Interfaces can contain methods, properties, events, and indexers as members. </paragraph>
4   <paragraph>The example <code_example><![CDATA[
5 interface IExample  
6 {  
7    string this[int index] { get; set; }  
8    event EventHandler E;  
9    void F(int value);  
10    string P { get; set; }  
11 }  
12 public delegate void EventHandler(object sender, EventArgs e);  
13 ]]></code_example>shows an interface that contains an indexer, an event E, a method F, and a property P. </paragraph>
14   <paragraph>Interfaces may employ multiple inheritance. In the example <code_example><![CDATA[
15 interface IControl  
16 {  
17    void Paint();  
18 }  
19 interface ITextBox: IControl  
20 {  
21    void SetText(string text);  
22 }  
23 interface IListBox: IControl  
24 {  
25    void SetItems(string[] items);  
26 }  
27 interface IComboBox: ITextBox, IListBox {}  
28 ]]></code_example>the interface IComboBox inherits from both ITextBox and IListBox. </paragraph>
29   <paragraph>Classes and structs can implement multiple interfaces. In the example <code_example><![CDATA[
30 interface IDataBound  
31 {  
32    void Bind(Binder b);  
33 }  
34 public class EditBox: Control, IControl, IDataBound  
35 {  
36    public void Paint() {...}  
37    public void Bind(Binder b) {...}  
38 }   
39 ]]></code_example>the class EditBox derives from the class Control and implements both IControl and IDataBound. </paragraph>
40   <paragraph>In the previous example, the Paint method from the IControl interface and the Bind method from IDataBound interface are implemented using public members on the EditBox class. C# provides an alternative way of implementing these methods that allows the implementing class to avoid having these members be public. Interface members can be implemented using a qualified name. For example, the EditBox class could instead be implemented by providing IControl.Paint and IDataBound.Bind methods. <code_example><![CDATA[
41 public class EditBox: IControl, IDataBound  
42 {  
43    void IControl.Paint() {...}  
44    void IDataBound.Bind(Binder b) {...}  
45 }  
46 ]]></code_example></paragraph>
47   <paragraph>Interface members implemented in this way are called explicit interface members because each member explicitly designates the interface member being implemented. Explicit interface members can only be called via the interface. For example, the EditBox's implementation of the Paint method can be called only by casting to the IControl interface. <code_example><![CDATA[
48 class Test  
49 {  
50    static void Main() {  
51       EditBox editbox = new EditBox();  
52       editbox.Paint();  // error: no such method  
53       IControl control = editbox;  
54       control.Paint();  // calls EditBox's Paint implementation  
55    }  
56 }  
57 ]]></code_example></paragraph>
58 </clause>