The difference between a class and a structure:
1. One is a reference type and the other is a value type
Class is a reference type, inherited from a class
Struct is a value type, inherited from a class, so it does not have polymorphism
Because structures are value types, assignments between structures can create new structures. When copying variables containing structures, all data is copied. Modifications made to the new copy will not change the data of the old copy. Classes are reference types, and assignments between classes are just copying references.
2. Differences in inheritance
Class supports inheritance, can be inherited from classes and interfaces, and is fully extensible unless sealed is declared. Otherwise, the class can inherit from the interfaces of other classes and can also be inherited.
Struct has no inheritance, cannot be inherited from another structure or class, and cannot be inherited itself. Although the structure is not explicitly declared with sealed, the structor implicitly sealed. Struct supports interface inheritance.
3. Internal structure:
structure
kind
There is no default (no parameter) constructor, but constructors can be added, structures can declare constructors, but they must be with parameters.
There is a default constructor
No destructor
Have a destructor
There is no abstract and sealed (because it cannot be inherited)
You can use abstract and sealed
There cannot be protected modifiers, and protected, virtual, sealed and override members cannot be declared.
Protected, virtual, sealed, override members can be declared
You can not use new initialization
You must use new initialization. The struct is initialized when declared. All member variables are defaulted to 0 or null.
Initializing the instance field in the structure is wrong
After the Struct variable is used, the memory allocation will be automatically unallocated.
Class instances have garbage collection mechanism to ensure memory recycling and processing
The objects created by Struct are simple to copy and can be directly connected with equal signs.
Class object replication is divided into shallow copy and deep copy, and it must be completed through special methods.
We can simply understand that class is a mobile machine with behavior, multiple units, and inheritance, while struct is a part box that combines parts of different structures. In fact, the most essential difference between class and struct is that class is a reference type, memory is allocated on the managed heap, while struct is a value type, and memory is allocated on the thread stack. This difference leads to all the differences mentioned above.
4. How to choose a class and a structure
(1) When implementing a structure that is mainly brave enough to store data, you can consider the structure
(2) Struct variables occupy the space of the stack, so they are suitable for situations where the amount of data is relatively small. The space of the stack is limited. For a large number of logical objects, creating classes is better than creating structures.
(3) Structural arrays have higher efficiency
(4) Structures represent objects as lightweight as points, rectangles, and colors. For example, if an array of objects containing 1000 points is declared, additional memory will be allocated to each object referenced, in which case the cost of the structure is lower.
(5) Classes are the best choice when expressing abstract and multi-level object levels
(6) In most cases, this type is just some data when the structurer is the best choice.
1. One is a reference type and the other is a value type
Class is a reference type, inherited from a class
Struct is a value type, inherited from a class, so it does not have polymorphism
Because structures are value types, assignments between structures can create new structures. When copying variables containing structures, all data is copied. Modifications made to the new copy will not change the data of the old copy. Classes are reference types, and assignments between classes are just copying references.
2. Differences in inheritance
Class supports inheritance, can be inherited from classes and interfaces, and is fully extensible unless sealed is declared. Otherwise, the class can inherit from the interfaces of other classes and can also be inherited.
Struct has no inheritance, cannot be inherited from another structure or class, and cannot be inherited itself. Although the structure is not explicitly declared with sealed, the structor implicitly sealed. Struct supports interface inheritance.
3. Internal structure:
structure
kind
There is no default (no parameter) constructor, but constructors can be added, structures can declare constructors, but they must be with parameters.
There is a default constructor
No destructor
Have a destructor
There is no abstract and sealed (because it cannot be inherited)
You can use abstract and sealed
There cannot be protected modifiers, and protected, virtual, sealed and override members cannot be declared.
Protected, virtual, sealed, override members can be declared
You can not use new initialization
You must use new initialization. The struct is initialized when declared. All member variables are defaulted to 0 or null.
Initializing the instance field in the structure is wrong
After the Struct variable is used, the memory allocation will be automatically unallocated.
Class instances have garbage collection mechanism to ensure memory recycling and processing
The objects created by Struct are simple to copy and can be directly connected with equal signs.
Class object replication is divided into shallow copy and deep copy, and it must be completed through special methods.
We can simply understand that class is a mobile machine with behavior, multiple units, and inheritance, while struct is a part box that combines parts of different structures. In fact, the most essential difference between class and struct is that class is a reference type, memory is allocated on the managed heap, while struct is a value type, and memory is allocated on the thread stack. This difference leads to all the differences mentioned above.
4. How to choose a class and a structure
(1) When implementing a structure that is mainly brave enough to store data, you can consider the structure
(2) Struct variables occupy the space of the stack, so they are suitable for situations where the amount of data is relatively small. The space of the stack is limited. For a large number of logical objects, creating classes is better than creating structures.
(3) Structural arrays have higher efficiency
(4) Structures represent objects as lightweight as points, rectangles, and colors. For example, if an array of objects containing 1000 points is declared, additional memory will be allocated to each object referenced, in which case the cost of the structure is lower.
(5) Classes are the best choice when expressing abstract and multi-level object levels
(6) In most cases, this type is just some data when the structurer is the best choice.