Structs and classes can also define fields, methods, and constructors, all of which can instantiate objects, so that structs and classes seem to function the same, but they are not the same on the data storage.
The difference between C # struct and class: The essential difference between these two data types is that they point to different memory locations. When passing a class, the main expression is whether the source object is changed at the same time.
1. Struct is a value type, class is a reference type
Although the initialization of the struct also uses the new operator, the struct object is still allocated on the stack instead of on the heap
When a class is passed, the content that is passed is in the managed memory location, and when the struct is passed, the content that is passed is the content that is located in the program's stack area. When a class passes an object modification, the source object is modified at the same time, and the source object is not affected when the transitive object of the struct is modified.
The efficiency of the stack is more efficient than the execution of the heap, but the stack has limited resources and is not suitable for dealing with large logical complex objects. So structure processing is a small object treated as a base type, whereas a class handles a business logic
Because structs are value types, assignments between structs can create new structures, and classes are reference types, and assignments between classes are just copy references
Note:
A. Although structs are different from classes, their base types are objects (object), and the base types of all types in C # are
B. Although the initialization of a struct also uses the new operator, the structure object is still allocated on the stack instead of on the heap, and if you do not use new, the field remains unassigned until all fields are initialized, and the object is not available
2. A struct cannot inherit from another struct or class, and itself cannot be inherited, although the structure is not explicitly declared with sealed, but the structure is implicit sealed
Class is fully extensible, unless the declared sealed is displayed otherwise the class can inherit other classes and interfaces, and itself can be inherited
Although structs cannot be inherited, structs can inherit interfaces, as are methods and class-inheriting interfaces.
3. Structure: cannot contain an explicit default constructor , no destructor, no abstract and sealed (because it cannot be inherited), cannot have protected modifier, can not be initialized with new
Initializing an instance field in a struct is an error
Class: There is a default constructor, there are destructors, you can use abstract and sealed, there are protected modifiers, you must use new initialization.
How do I choose a struct or a class
After discussing the similarities and differences between structs and classes, here's how to choose whether to use structs or classes:
1. Stack space is limited, for a large number of logical objects, creating a class is better than creating a structure
2. Structures represent lightweight objects such as points, rectangles, and colors, for example, if you declare an array that contains 1000 point objects, additional memory is allocated for each object that is referenced. In this case, the cost of the structure is lower.
3. Classes are the best choice when performing abstract and multilevel object hierarchies
4. Most of the time the type is just some data when the structure is the best choice
The differences between structs and classes in C #