C #: differences between structures and Classes,
I. Structure: Value Type, stored in the stack, located in the memory logic area of the computer class: reference type, stored in the heap, different logical locations in computer memory 2. Small Data use structure; When a structure value is passed to a method, the entire data structure is passed; a class is passed, in fact, it is to pass the reference to the object, that is, only the memory address. to modify the structure, it changes the structure copy, which is the definition of the Value Type working method: the copy of the transfer value; passing a reference to the class means modifying the value in the class. Actually, the original object is changed. 3. Code Example 1. create PointClass. cs
1 namespace StructAndClass 2 { 3 internal class PointClass 4 { 5 public PointClass(int x, int y) 6 { 7 X = x; 8 Y = y; 9 }10 11 public int X { get; set; }12 13 public int Y { get; set; }14 }15 }
2. Create PointStruct. cs
1 namespace StructAndClass 2 { 3 internal struct PointStruct 4 { 5 public int X { get; set; } 6 7 public int Y { get; set; } 8 9 public PointStruct(int x, int y)10 {11 X = x;12 Y = y;13 }14 }15 }
3. Program. cs
1 using System; 2 3 namespace StructAndClass 4 {5 internal class Program 6 {7 private static void Main (string [] args) 8 {9 Console. writeLine ("PointStruct ===="); 10 var pStruct = new PointStruct (10, 10); 11 Console. writeLine ("Initial Value: x = {0}, y = {1}", pStruct. x, pStruct. y); 12 ModifyPointStruct (pStruct); 13 Console. writeLine ("value after ModifyPointStruct () is called: x = {0}, y = {1}", pStruct. x, pStruct. y); 14 Console. writeLine (); 15 16 Console. writeLine ("PointClass ===="); 17 var pClass = new PointClass (10, 10); 18 Console. writeLine ("Initial Value: x = {0}, y = {1}", pClass. x, pClass. y); 19 ModifyPointClass (pClass); 20 Console. writeLine ("value after ModifyPointClass () is called: x = {0}, y = {1}", pClass. x, pClass. y); 21 Console. read (); 22} 23 24 private static void ModifyPointStruct (PointStruct point) 25 {26 Console. writeLine ("Call method: ModifyPointStruct"); 27 point. X = 20; 28 point. Y = 20; 29 Console. writeLine ("modified value: x = {0}, y = {1}", point. x, point. y); 30} 31 32 private static void ModifyPointClass (PointClass point) 33 {34 Console. writeLine ("Call method: ModifyPointClass"); 35 point. X = 20; 36 point. Y = 20; 37 Console. writeLine ("modified value: x = {0}, y = {1}", point. x, point. y); 38} 39} 40}
4. Results:
[Resolution]
When ModifyPointStruct (PointStruct point) is called, only the structure copies are modified, so the original structure does not change;
When ModifyPointClass (PointClass point) is called, the modified object is the original object, because the parameter is passed as a reference address, which points to the original object
Iv. Summary
The structure is a value type and passed in the stack. Each modification using a method only involves a structure copy;
As for the class, the reference of the memory address is passed, and the initial value is modified.