Structures are very similar to classes, and can also contain data members and function members, but unlike classes, structures are a value type (we can understand them as a special value type so there is no inheritance problem) allocating data to them does not require allocating memory from the managed heap. A variable of a structure type directly contains the data of the structure, while a variable of a class type only contains a reference to the corresponding object.
The following is a summary of the differences between structures and classes:
1. Structure is a value type, and assigning a value to a variable of the structure type will create a copy of the assigned value.
2. The default value of the structure instance is not null, but the initial value with the default value.
3. The meaning of this is different in structure and class.
4. Structure does not support inheritance (so the declaration accessibility of structure members cannot be protected or protected internal, and the function members in the structure cannot be abstract or virtual, so in the structure the override modifier is only suitable for overriding the inherited methods) but the interface can be implemented.
5. In the instance field declaration in the structure, the initial value setting item cannot be contained in the variable.
6. An instance constructor without parameters cannot be declared in a structure.
7. The destructor cannot be declared in the structure.
Test the difference feature code:
using System; namespace StructAndClass { struct SPoint { public int x, y; public SPoint(int x, int y) { = x; = y; } } class CPoint { public int x, y; public CPoint(int x, int y) { = x; = y; } } class Test { public static void Main() { SPoint sp1 = new SPoint(2, 5); ("Structure/sp1 initial value:"); ("={0}", ); SPoint sp2 = sp1; ("sp1=sp2back:"); ("={0}"); ("={0}", ); ("={0}", ); = 5; ("After changing the value of sp1 again:"); ("={0}", ); ("={0}", ); ("============================"); CPoint cp1 = new CPoint(2,5); ("Class/cp1 initial value:"); ("={0}", ); CPoint cp2 = cp1; ("cp1=cp2back:"); ("={0}", ); ("={0}", ); = 5; ("After changing the value of cp1 again:"); ("={0}", ); ("={0}", ); (); } } }
For structures, even structure variables declared without the new operator are valid. Although structures cannot declare a parameterless constructor, they actually implicitly contain a parameterless constructor: and the instance function members of the structure cannot be called before all fields of the structure are explicitly assigned. All fields should be assigned values in the constructor of the structure. For example:
struct DC { public int x, y; public int X { set { x = value; } get { return x; } } public int Y { set { y = value; } get { return y; } } public DC(int x,int y) { = x; = y; } } struct RDC { public int x, y; public int X { set { x = value; } get { return x; } } public int Y { set { y = value; } get { return y; } } public RDC(int x, int y) { = x; = y; } } class Test { public static void Main() { DC dc=new DC(); = 3; = 5; ("After initializing x,y:"); ("You can access and modify it now={0}Value of",); ("I created a DC copy structure RDC/n here and it will report an error if it is not initialized with x and y"); RDC rdc; = 5;//It can be compiled and passed = 3;//An error will occur here, and it cannot be compiled and passed ("=======test over================"); } }
In the instance constructor of a structure, this is equivalent to an out parameter of a structure type, (it must be assigned explicitly internally) and within the instance function members of the structure, this is equivalent to a ref parameter of a structure type. In both cases, this itself is equivalent to a variable, so it is possible to modify the entire structure involved in the call of the function member (such as assigning this, or passing this as a ref or out parameter). For example: (citing the above example)
struct DC { public int x, y; public int X { set { x = value; } get { return x; } } public int Y { set { y = value; } get { return y; } } public DC(int x,int y) { X= x;//This is wrong Y = y;//It will prompt that no values are assigned to this object's fields } }