Jon Jagger
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.1 Value semantics Paragraph 11 Structs are value types (§11.1) and are said to have value semantics. 2 Classes, on the other hand, are reference types (§11.2) and are said to have reference semantics. Paragraph 21 A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains a reference to the data, the latter known as an object. Paragraph 31 With classes, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. 2 With structs, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other. 3 Furthermore, because structs are not reference types, it is not possible for values of a struct type to be null. [Example: Given the declaration
struct Point  
   public int x, y;  
   public Point(int x, int y) {  
      this.x = x;  
      this.y = y;  
the code fragment
Point a = new Point(10, 10);  
Point b = a;  
a.x = 100;  
outputs the value 10. The assignment of a to b creates a copy of the value, and b is thus unaffected by the assignment to a.x. Had Point instead been declared as a class, the output would be 100 because a and b would reference the same object. end example]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS