Structure
Sometimes we only need a small data structure, and the class provides more functions than we need; considering performance reasons, it is best to use structures.
A structure is a value type, stored in the stack or stored as inline (if the structure is part of another object stored in the heap).
For example class:
public class Dimensions { public Dimensions(double length, double width) { Length = length; Width = width; } public double Length { get; set; } public double Width { get; set; } }
You can use struct to replace:
public struct Dimensions { public Dimensions(double length, double width) { Length = length; Width = width; } public double Length { get; set; } public double Width { get; set; } }
You can also create functions for structs, which are exactly the same as creating functions for classes:
public struct Dimensions { public Dimensions(double length, double width) { Length = length; Width = width; } public double Length { get; set; } public double Width { get; set; } public double Diagonal => (Length * Length + Width * Width); }
Structure initialization
Structures are value types, but their syntax and classes are basically the same when used. For example, for the classes or structures defined above, you can use code:
Dimensions point = new Dimensions(3, 6);
Notice:
Because structs are value types, the new operator works differently than classes and other reference types.
The new operator used for struct struct does not allocate memory in the heap, but only calls the corresponding constructor and initializes all fields according to the parameters passed to it.
For structures, variable declarations are actually Wei the entire structure allocates space in the stack. For example, use the following syntax (if it is a class, it will compile errors):
Dimensions point; //Open new directly = 3; = 6;
Structures follow the rules that other data types follow: all elements must be initialized before use.
Initialization method of structure:
- Use new operator
- Assign values to all fields directly
Structural performance impact
- When allocating memory to structures, it is very fast because they will be inlined or saved on the stack. When the structure is deleted outside scope, it is also very fast, and there is no need to wait for garbage collection.
- If you pass the structure as a parameter, or assign one structure to another, all the contents of the structure will be copied; this will result in performance losses. This is why structures are mainly used for small data structures.
- When passing a structure as a parameter to a method, it should be passed as a ref parameter - only the address of the structure in memory is passed at this time.
Read-only structure
Starting from C# 7.2, the readonly modifier can be applied to the structure struct, so the compiler ensures the invariance of the structure.
public readonly struct Dimensions { public Dimensions(double length, double width) { Length = length; Width = width; } public double Length { get; } public double Width { get; } public double Diagonal => (Length * Length + Width * Width); }
For readonly modifier, if the type changes the field or property after the object is created, the compiler will report an error.
Use the readonly compiler to generate optimized code so that it does not copy the contents of the structure when passing it;
Instead, the compiler uses references because it never changes.
Note: The properties of the readonly structure above are read-only and only get; if there is set, the compilation will report an error:
error CS8341: Auto-implemented instance properties in readonly structs must be readonly.
Structures and Classes - Value Types and Reference Types
using System; namespace value_reference { public struct A { public int x { get; set; } } public class B { public int x { get; set; } } class Program { public static void UpdateStructValue(A a) { = 10; } public static void UpdateObjectValue(B b) { = 10; } static void Main(string[] args) { var a = new A { x = 1 }; var b = new B { x = 1 }; UpdateStructValue(a); UpdateObjectValue(b); ($" -> {}"); ($" -> {}"); } } }
Output result:
$ dotnet run
-> 1
-> 10
The above is a brief analysis of the detailed content of C# structure struct. For more information about C# structure struct, please pay attention to my other related articles!