I have read the theory of Struct better, but I have basically not applied it in my work, and Class is everywhere. Is Struct no use value? SearchedHow to make choices in classes and structures?
✔️ If the instance of a type is small and usually has a short life or is usually embedded in other objects, consider defining a structure rather than a class.
❌ Avoid defining structures unless the type has all the following characteristics:
It logically represents a single value, similar to the original type (int, double, etc.).
Its instance size is less than 16 bytes.
It is immutable.
It doesn't have to be packed frequently.
When developing software, there are often page pop-ups, and the main page often needs to pass some parameters into the form. The form only uses these parameters to search for other data display, or uses it when saving data, that is, only those parameters are read into the parameters. Although multiple parameters can be passed through constructor parameters, it is generally better to encapsulate multiple parameters into one whole. If these parameters can be regarded as a small logical unit in business, it should be better to encapsulate them into Struct.
eg: The function passes the parameter, and the struct will be copied again, so add ref.
using System; using ; namespace WindowsFormsApp1 { public partial class Form1 : Form { private FundStruct s_fundStruct; public Form1() { InitializeComponent(); } public Form1(ref FundStruct fundStruct) { InitializeComponent(); s_fundStruct = fundStruct; } private void Form1_Load(object sender, EventArgs e) { this. = s_fundStruct.Code; this. = s_fundStruct.ShortName; ... } } public struct FundStruct { /// <summary> /// Encoding /// </summary> internal readonly string Code; /// <summary> /// Inner code number /// </summary> internal readonly int Number; /// <summary> /// Abbreviation /// </summary> internal readonly string ShortName; /// <summary> /// Full name /// </summary> internal readonly string LongName; internal FundStruct(string code, int number, string shortName, string longName) { Code = code ?? throw new ArgumentNullException(nameof(code)); Number = number; ShortName = shortName ?? throw new ArgumentNullException(nameof(shortName)); LongName = longName ?? throw new ArgumentNullException(nameof(longName)); } } }
Differences from classes:
Structural types and class types in C# are very syntax similar. They are both data structures that can include data members and method members.
The difference between structure and class:
1. Structure is a value type, which allocates space in the stack; while class is a reference type, which allocates space in the heap, and only references are stored in the stack.
2. The structure type directly stores member data, so that the data of other classes is located in the heap. The variables located in the stack store references to the data objects in the heap.
Simple types in C#, such as int, double, bool, etc., are all structural types. If needed, you can even use structure types combined with operators to operate overloading, and then create a new value type for the C# language.
Since structures are value types and directly store data, using structures will bring better performance when the main members of an object are data and the amount of data is small.
Because structures are value types, performance is very good when allocating memory to structures, or when structures are removed outside scope, because they will be inlined or saved on the stack. When a variable of one structure type is assigned to another structure, the impact on performance depends on the size of the structure. If the structure has a large number of data members and is complex, it will cause losses. Next, use a piece of code to illustrate this problem.
Analysis of applicable occasions of structure and class:
1. When the stack space is very limited and there are a large number of logical objects, creating classes is better than creating structures;
2. For lightweight objects such as points, rectangles and colors, if you want to declare an array of objects with many color, the CLR needs to allocate memory for each object. In this case, the cost of using the structure is lower;
3. When expressing abstract and multi-level object levels, classes are the best choice because the structure does not support inheritance.
4. In most cases, the target type only contains some data, or is mainly data.
The above is the detailed content of some problem analysis of c# Struct. For more information about c# Struct, please pay attention to my other related articles!