Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / docs / ecma334 / 8.7.xml
1 <?xml version="1.0"?>
2 <clause number="8.7" title="Classes" informative="true">
3   <paragraph>Class declarations define new reference types. A class can inherit from another class, and can implement interfaces. </paragraph>
4   <paragraph>Class members can include constants, fields, methods, properties, events, indexers, operators, instance constructors, destructors, static constructors, and nested type declarations. Each member has an associated accessibility (<hyperlink>10.5</hyperlink>), which controls the regions of program text that are able to access the member. There are five possible forms of accessibility. These are summarized in the table below. <table_line>Form Intuitive meaning </table_line>
5 <table_line>public Access not limited </table_line>
6 <table_line>protected Access limited to the containing class or types derived from the containing class </table_line>
7 <table_line>internal Access limited to this program </table_line>
8 <table_line>protected </table_line>
9 <table_line>internal </table_line>
10 <table_line>Access limited to this program or types derived from the containing class </table_line>
11 <table_line>private Access limited to the containing type </table_line>
12 </paragraph>
13   <paragraph>The example <code_example><![CDATA[
14 using System;  
15 class MyClass  
16 {  
17    public MyClass() {  
18       Console.WriteLine("Instance constructor");  
19    }  
20    public MyClass(int value) {  
21       MyField = value;  
22       Console.WriteLine("Instance constructor");  
23    }  
24    ~MyClass() {  
25       Console.WriteLine("Destructor");  
26    }  
27    public const int MyConst = 12;  
28    public int MyField = 34;  
29    public void MyMethod(){  
30       Console.WriteLine("MyClass.MyMethod");  
31    }  
32    public int MyProperty {  
33       get {  
34          return MyField;  
35       }  
36       set {  
37          MyField = value;  
38       }  
39    }  
40    public int this[int index] {  
41       get {  
42          return 0;  
43       }  
44       set {  
45          Console.WriteLine("this[{0}] = {1}", index, value);  
46       }  
47    }  
48    public event EventHandler MyEvent;  
49    public static MyClass operator+(MyClass a, MyClass b) {  
50       return new MyClass(a.MyField + b.MyField);  
51    }  
52    internal class MyNestedClass  
53    {}  
54 }  
55 ]]></code_example>shows a class that contains each kind of member. The example <code_example><![CDATA[
56 class Test  
57 {  
58    static void Main() {  
59       // Instance constructor usage  
60       MyClass a = new MyClass();  
61       MyClass b = new MyClass(123);  
62       // Constant usage  
63       Console.WriteLine("MyConst = {0}", MyClass.MyConst);  
64       // Field usage  
65       a.MyField++;  
66       Console.WriteLine("a.MyField = {0}", a.MyField);  
67       // Method usage  
68       a.MyMethod();  
69       // Property usage  
70       a.MyProperty++;  
71       Console.WriteLine("a.MyProperty = {0}", a.MyProperty);  
72       // Indexer usage  
73       a[3] = a[1] = a[2];  
74       Console.WriteLine("a[3] = {0}", a[3]);  
75       // Event usage  
76       a.MyEvent += new EventHandler(MyHandler);  
77       // Overloaded operator usage  
78       MyClass c = a + b;  
79    }  
80    static void MyHandler(object sender, EventArgs e) {  
81       Console.WriteLine("Test.MyHandler");  
82    }  
83    internal class MyNestedClass  
84    {}  
85 }  
86 ]]></code_example>shows uses of these members. </paragraph>
87 </clause>