SoFunction
Updated on 2025-04-08

.NET6 new struct optimization

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 versionrecordyesclassis a reference type, butrecord structIt is a value type,

It is used as follows:

record struct Point(int X, int Y);

exist.NET6It also supports record to declare a based onclassofrecord, this is the same as the original record, for examplerecord classRecordModel(int Id, string Name) ​​`​ This and ​​`​​record RecordModel(int Id, string Name)​​ record structEquals andGetHashCodeAnd 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 usereadonlyTo mark the structure, you can also use itreadonly struct record,butrecord structRef modification cannot be used. usereadonly struct recordThe declared structure, if the corresponding attribute of the Primary Constructor is used, it will be init. For examplereadonly 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

.NET6Support 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 heredefaultandnewThe difference,defaultIt is an empty state of the structure, and no parameter structure will not be executed.newIt 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 fromrecordIn addition, .NET6 also extends the usage of with expressions, and ordinary structures and anonymous objects can also be used.withTo 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}

withOnly 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 classCompared to record struct, there is no Clone method, because struct does not need to have its own Clone function.record structClone 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!