dlr bug
[mcs.git] / docs / ecma334 / 18.3.8.xml
blob6e358f283c5d87caed9403b9db3e8b656e4ec045
1 <?xml version="1.0"?>
2 <clause number="18.3.8" title="Constructors">
3   <paragraph>Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, every struct implicitly has a parameterless instance constructor, which always returns the value that results from setting all value type fields to their default value and all reference type fields to null (<hyperlink>11.1.1</hyperlink>). A struct can declare instance constructors having parameters. <example>[Example: For example <code_example><![CDATA[
4 struct Point  
5 {  
6    int x, y;  
7    public Point(int x, int y) {  
8       this.x = x;  
9       this.y = y;  
10    }  
11 }  
12 ]]></code_example></example></paragraph>
13   <paragraph>
14     <example>Given the above declaration, the statements <code_example><![CDATA[
15 Point p1 = new Point();  
16 Point p2 = new Point(0, 0);  
17 ]]></code_example>both create a Point with x and y initialized to zero. end example]</example>
18   </paragraph>
19   <paragraph>A struct instance constructor is not permitted to include a constructor initializer of the form base(...). </paragraph>
20   <paragraph>The this variable of a struct instance constructor corresponds to an out parameter of the struct type, and similar to an out parameter, this must be definitely assigned (<hyperlink>12.3</hyperlink>) at every location where the constructor returns. <example>[Example: Consider the instance constructor implementation below: <code_example><![CDATA[
21 struct Point  
22 {  
23    int x, y;  
24    public int X {  
25       set { x = value; }  
26    }  
27    public int Y {  
28       set { y = value; }  
29    }  
30    public Point(int x, int y) {  
31       X = x;    // error, this is not yet definitely assigned  
32       Y = y;    // error, this is not yet definitely assigned  
33    }  
34 }  
35 ]]></code_example></example></paragraph>
36   <paragraph>
37     <example>No instance member function (including the set accessors for the properties X and Y) can be called until all fields of the struct being constructed have been definitely assigned. Note, however, that if Point were a class instead of a struct, the instance constructor implementation would be permitted. end example]</example>
38   </paragraph>
39 </clause>