Struct can all be replaced by class. Why should I use struct? The existence is reasonable. struct has many advantages in performance. Let's take a look at the main differences between them?
- The data types are different. struct is the value type, and class is the reference type. Therefore, they have differences between all value types and reference types. Because the stack execution efficiency is higher than the heap execution efficiency, but the stack resources are limited, it is not suitable for processing large objects with complicated logic, therefore, struct is often used to process small objects that are treated as base types, while class is used to process a certain business logic.
- In terms of inheritance, struct can neither inherit nor be inherited, but can implement interfaces, and class can be fully extended.
- The internal structure is different. struct can only add constructor with parameters. It cannot use abstract or protected modifiers or initialize instance fields. However, struct can override system. the three Virtual Methods of the object, equals (), tostring (), gethashtable (), and class do not have these restrictions.
Comparing the differences between struct and class, the following usage principles of struct and class can be obtained:
1 struct is preferred when it is used to represent lightweight objects such as vertices and rectangles that are mainly used to store data.
2. Class is preferred when it represents large objects with large data volumes and complex logic.
3. Class is the best choice for presentation of abstract and multi-level object Layers
Http://blog.csdn.net/LOVESONGFOREVER/archive/2009/11/06/4775934.aspx