Home >Backend Development >C++ >Structs vs. Classes in C#: When Should You Choose Which?
When will the structure (struct) and the class (class)
In C#, when selecting the structure or class, please consider the following principles:
When to use the structure:
This type represents a single value, similar to basic types (for example, integer and dual -precision floating point number).
This type represents multiple or complex data structures.
The size is greater than 16 bytes.Example
.NET Framework's
class uses the internal structure as itsand type. These structures follow the above principles because they:
indicates a single value (key value pair and enumerator state). Dictionary<TKey, TValue>
Entry
very small (less than 16 bytes). Enumerator
When the structure is implemented, it will be a reference type when it is converted to the interface.
Should deal with the type of value responsible to avoid accidental copying or modification.
The performance advantages of Structure are mainly reflected in the scenario involving a large number of or frequent creation and access to small data items.
The above is the detailed content of Structs vs. Classes in C#: When Should You Choose Which?. For more information, please follow other related articles on the PHP Chinese website!