Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 18 Structsprevious at this levelnext at this level 18.3 Class and struct differencesprevious at this levelnext at this level 18.3.8 Constructors Paragraph 11 Unlike a class, a struct is not permitted to declare a parameterless instance constructor. 2 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 (§11.1.1). 3 A struct can declare instance constructors having parameters. [Example: For example
struct Point  
{  
   int x, y;  
   public Point(int x, int y) {  
      this.x = x;  
      this.y = y;  
   }  
}  
Given the above declaration, the statements
Point p1 = new Point();  
Point p2 = new Point(0, 0);  
both create a Point with x and y initialized to zero. end example]
Paragraph 21 A struct instance constructor is not permitted to include a constructor initializer of the form base(...). Paragraph 31 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 (§12.3) at every location where the constructor returns. [Example: Consider the instance constructor implementation below:
struct Point  
{  
   int x, y;  
   public int X {  
      set { x = value; }  
   }  
   public int Y {  
      set { y = value; }  
   }  
   public Point(int x, int y) {  
      X = x;    // error, this is not yet definitely assigned  
      Y = y;    // error, this is not yet definitely assigned  
   }  
}  
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]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS