Some optimizations have been made for Struct in .NET6. Let’s take a look at the optimizations for Struct in .NET6 through some cases.
1. Record Struct
Although it was found in the previous versionrecord
, but the previous versionrecord
yesclass
is a reference type, butrecord
struct
It is a value type,
It is used as follows:
record struct Point(int X, int Y);
exist.NET6
It also supports record to declare a based onclass
ofrecord
, this is the same as the original record, for examplerecord class
RecordModel(int Id, string Name) ` This and `record RecordModel(int Id, string Name) record struct
Equals andGetHashCode
And override the == and != operators, and you can use the with modified partial properties to create new objects. If the record struct declares a parameter constructor, an implicit parameterless construct is generated.
The code is as follows:
var p1 = new Point(1, 2); var p2 = p with { X = 2 }; (p1); (p2); (new Point());
Run the above code to see that even if the parameterless construct is not explicitly declared, a parameterless construct will be generated to initialize.
The above code output is as follows:
Point { X = 1, Y = 2 } Point { X = 2, Y = 2 } Point { X = 0, Y = 0 }
2. readonly struct record
We can usereadonly
To mark the structure, you can also use itreadonly struct record
,butrecord struct
Ref modification cannot be used. usereadonly struct record
The declared structure, if the corresponding attribute of the Primary Constructor is used, it will be init. For examplere
adonly record struct Point(int X, int Y);
The declaration of the attribute is like this:
internal readonly struct Point : IEquatable { public int X { get; init; } public int Y { get; init; } public Point(int X, int Y) { = X; = Y; } }
3. Parameterless Constructor
.NET6
Support user-defined parameterless construction method, we can add initialization logic to the parameterless construction method.
The code is as follows:
(new Point1().ToString()); (default(Point1).ToString()); (()); struct Point1 { public int X { get; set; } public int Y { get; set; } private int Z { get; set; } public Point1() { X = 1; Y = 2; Z = 3; } public override string ToString() { return $"{X}_{Y}_{Z}"; } }
Pay attention heredefault
andnew
The difference,default
It is an empty state of the structure, and no parameter structure will not be executed.new
It will execute, and when creating objects through reflection, it will also execute the construction.
The code output results are as follows:
1_2_3
0_0_0
1_2_3
Apart fromrecord
In addition, .NET6 also extends the usage of with expressions, and ordinary structures and anonymous objects can also be used.with
To modify some properties
The code is as follows:
((new Point1() with { X = 2 }).ToString()); (); var obj = new { X = 1, Y = 1 }; ((obj)); ((obj with { X = 3, Y = 3 }));
The output result is as follows:
2_2_3
{"X":1,"Y":1}
{"X":3,"Y":3}
with
Only operations can be performed on public members. The Z in the above code is private, so it cannot be specified in the with expression. andrecord class
Compared to record struct, there is no Clone method, because struct does not need to have its own Clone function.record struct
Clone member methods are not allowed to be declared, and all records are not allowed to be declared.
This is the end of this article about NET6 new special struct optimization. For more related NET6 struct optimization content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!