13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 18 Structsstruct Point{int x, y;public Point(int x, int y) {this.x = x;this.y = y;}}Given the above declaration, the statementsPoint p1 = new Point();Point p2 = new Point(0, 0);both create a Point with x and y initialized to zero. end example]A struct instance constructor is not permitted to include a constructor initializer of the form base(…).The this variable of a struct instance constructor corresponds to an out parameter of the struct type, andsimilar to an out parameter, this must be definitely assigned (§12.3) at every location where theconstructor 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 assignedY = 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 allfields of the struct being constructed have been definitely assigned. Note, however, that if Point were aclass instead of a struct, the instance constructor implementation would be permitted.end example]18.3.9 DestructorsA struct is not permitted to declare a destructor.18.3.10 Static constructorsStatic constructors for structs follow most of the same rules as for classes. The execution of a staticconstructor for a struct is triggered by the first of the following events to occur within an applicationdomain:• An instance member of the struct is referenced.• A static member of the struct is referenced.• An explicitly declared constructor of the struct is called.[Note: The creation of default values (§18.3.4) of struct types does not trigger the static constructor. (Anexample of this is the initial value of elements in an array.) end note]18.4 Struct examplesThis whole clause is informative.271

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!