A structure is a data type defined by a programmer and is very similar to a class. Has data members and function members.
But there are also differences:
1) the class is the reference type, and the structure is the value type;
2) The structure is implicitly sealed and cannot be derived;
Syntax declaration is similar to class:
// Schema declaration struct StructName {// member variables included}
See the following sample code to demonstrate how to use the C # structure:
static void Main(string[] args) { Point first, second, third; first.x = 10; first.y = 10; second.x = 20; second.y = 20; third.x = first.x + second.x; third.y = first.y + second.y; Console.WriteLine("first:{0},{1}",first.x,first.y); Console.WriteLine("second:{0},{1}",second.x,second.y); Console.WriteLine("third:{0},{1}",third.x,third.y); Console.ReadKey(); } struct Point { public int x; public int y; }
Structure is Value Type
1) a variable of the structure type cannot be null;
2) Two Structure Variables cannot reference the same object
Static void Main (string [] args) {CSimple cs1 = new CSimple (), cs2 = null; Simple ss1 = new Simple (), ss2 = new Simple (); cs1.x = ss1.x = 5; cs1.y = ss1.y = 10; cs2 = cs1; // value assignment class instance ss2 = ss1; // value assignment structure instance} class CSimple {public int x; public int y;} struct Simple {public int x; public int y ;}
Create a CSimple class and a Simple structure
After the Main () instance declares two variables for them respectively, cs1 and cs2 point to references in the heap, while ss1 and ss2 allocate space and store in the stack respectively.
Assign a structure to another structure, that is, repeat the value from a structure to another structure. Unlike a class, when copying a class variable, only the reference is copied.
In the code above, after the class assignment is complete, cs2 and cs1 point to the same object in the heap. However, after the Structure assignment is complete, the value of the ss2 member is the same as that of the ss1 member.
Constructor and destructor in the Structure
The language implicitly provides a non-parameter constructor for each structure. This constructor sets each member of the structure to the default value of this type, and the referenced member is set to null.
A predefined non-parameter constructor exists for each structure and cannot be deleted or redefined. However, you can create another constructor as long as they have parameters. This is different from the class. For a class, the compiler only provides an implicit non-parameter constructor when there is no other constructor declaration.
To call a constructor, including an implicit non-parameter constructor, use the new operator. The new operator is used even if memory is not allocated from the heap.
Example:
Static void Main (string [] args) {Simple s1 = new Simple (); // call the implicit constructor Simple s2 = new Simple (5, 10 ); // call the constructor Console. writeLine ("{0}, {1}", s1.x, s1.y); Console. writeLine ("{0}, {1}", s2.x, s2.y); Console. readKey () ;}struct Simple {public int x; public int y; public Simple (int a, int B) {x = a; y = B ;}}
It may not apply to instances where the new operator creates a structure. However, there are some restrictions:
1) the value of the data member cannot be used until it is set
2) You cannot call any function member until all data members have been assigned a value.
Static void Main (string [] args) {Simple s1, s2; Console. writeLine ("{0}, {1}", s1.x, s1.y); // compilation error. s1.x, s1.y have not been assigned s2.x = 50; s2.y = 10; Console. writeLine ("{0}, {1}", s2.x, s2.y); Console. readKey () ;}struct Simple {public int x; public int y ;}