好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

.NET6新特新 struct优化

在.NET6中针对Struct做了一些优化,下面我们就通过一些案例来看一下.NET6中针对Struct的优化。

一、Record Struct

虽然在上一个版本中就有了 record ,但是上一版本中的的 record 是 class 是一个引用类型,但是 record struct 是值类型是一个结构,

它的使用方式如下:

?

1

record struct Point(int X, int Y);

在 .NET6 中也支持record来声明一个基于 class 的 record ,这和原来的record是一样的,例如? ? record class RecordModel(int Id, string Name) ??`? ?这和 ??`? ?record RecordModel(int Id, string Name)?? record struct 会自动生成Equals和 GetHashCode 并重写==和!=操作符,并且可以用with修改部分属性创建新的对象。如果record struct声明有参数构造器,则会生成一个隐式的无参构造。

代码如下:     

?

1

2

3

4

5

6

7

8

9

var p1 = new Point(1, 2);

 

var p2 = p with { X = 2 };

 

Console.WriteLine(p1);

 

Console.WriteLine(p2);

 

Console.WriteLine(new Point());

运行上述代码可以看到即使没有显式声明无参构造还是会生成一个无参构造来初始化。

上述代码输出如下:

?

1

2

3

4

5

Point { X = 1 , Y = 2 }

 

Point { X = 2 , Y = 2 }

 

Point { X = 0 , Y = 0 }

二、readonly struct record

我们可以使用 readonly 来标记结构体,也可以使用 readonly struct record ,但 record struct 不能使用ref修饰。使用 readonly struct record 声明的结构体,如果使用Primary Constructor对应的属性会是init。例如? ? re adonly record struct Point(int X, int Y);??

属性的声明是这样的:

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

internal readonly struct Point : IEquatable

 

{

 

   public int X { get; init; }

 

   public int Y { get; init; }

 

   public Point(int X, int Y)

 

   {

 

   this.X = X;

 

   this.Y = Y;

 

   }

 

}

三、Parameterless Constructor

.NET6 支持用户自定义无参构造方法,我们可以在无参构造方法中加入初始化逻辑,

代码如下如下:

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

Console.WriteLine(new Point1().ToString());

 

Console.WriteLine(default(Point1).ToString());

 

Console.WriteLine(Activator.CreateInstance());

 

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}" ;

 

     }

 

}

这里需要注意 default 和 new 的差别, default 是结构体空状态,不会执行无参构造, new 是会执行,通过反射创建对象的时候也会执行构造,

代码输出结果如下:

1_2_3

0_0_0

1_2_3

除了 record 之外,.NET6还扩展了with表达式用法,普通结构体和匿名对象也可以使用 with 来修改部分属性

代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Console.WriteLine((new Point1() with { X = 2 }).ToString());

 

Console.WriteLine();

 

var obj = new

 

{

 

     X = 1,

 

     Y = 1

 

};

 

Console.WriteLine(JsonSerializer.Serialize(obj));

 

Console.WriteLine(JsonSerializer.Serialize(obj with { X = 3, Y = 3 }));

输出结果如下:

2_2_3

{"X":1,"Y":1}

{"X":3,"Y":3}

with 只能对public成员进行操作,上面代码中的Z是private,因此在with表达式中是不能指定。 和 record class 相比record struct没有 Clone 方法,因为struct不需要自带Clone功能, record struct 不允许声明Clone成员方法,所有record都不允许声明Clone 成员。

到此这篇关于 NET6新特新 struct优化的文章就介绍到这了,更多相关 NET6 struct优化内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.tuicool.com/articles/aEBJBfv

dy("nrwz");

查看更多关于.NET6新特新 struct优化的详细内容...

  阅读:75次