Class and struct syntax are almost the same, but they are fundamentally different: class is a reference type, a struct is a value type, that is, in memory, it is stored in a different way, resulting in a series of differences.
Class is the basic concept of object-oriented programming, and is a custom data structure type that typically contains fields, properties, methods, constructors, indexers, events, and so on. In. NET, all classes inherit from the System.Object class, which is a reference type, that is, when we new an instance of a class, the object holds the reference address of the actual data for that instance, and the value of the object is saved in the managed heap.
A struct (struct) is a value type that organizes a set of related information variables into a single variable entity. All structures inherit from the System.ValueType class, which is a type of value, and we can use structs like int and char. A struct instance is allocated on the stack of threads, and it itself stores a value instead of a pointer to that value.
Knowing the nature of class and struct, we can summarize the differences between them by analyzing and experimenting:
(1) About substance: class is a reference type, struct is a value type
(2) About functions: class is the encapsulation of behavior, used to express objects, and structs are the encapsulation of data used to store data
(3) About inheritance: class supports inheriting from classes and interfaces, whereas structs can only inherit interfaces. Structs cannot inherit from class, nor can they be used as the base class for classes.
(4) About constructors: class can declare a parameterless constructor, declare a destructor, and a struct can only declare a constructor with a parameter and cannot declare a destructor.
(5) About instantiation: class is to use the new keyword, and struct can not use the new keyword, struct is initialized during declaration, all member variables default to 0 or null.
(6) About abstraction: class can be a real abstract class (abstract), you can declare abstract functions, and structs cannot.
(7) About overloading: class can declare protected members, virtual members, sealed members, and override members, while structs are not, but it is worth noting that Structs can overload the 3 virtual methods of System.Object, Equals (), ToString (), and gethashtable ().
(8) On the comparison: two classes are compared with equals and = = Two, that is, the value of equality and reference the same, and two structs can be directly through the = = to judge.
(9) About destruction: class instances are garbage-collected to ensure that memory is recycled, and the struct variables are automatically deallocated immediately after they are used.
(10) About parameter passing: class variables are passed by address, whereas struct variables are passed by value.
Since class can almost completely replace structs for all functions, is there any need for structs? We should consider using structs instead of class, at least in the following cases:
- When implementing a structure for storing data, you can consider a struct.
- Consider a struct when you are considering compatibility issues with some unmanaged code communication.
[Basic knowledge of C #] The difference between class and struct