我是靠谱客的博主 酷炫冬天,这篇文章主要介绍.NET6新特新 struct优化,现在分享给大家,希望可以做个参考。

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

一、Record Struct

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

它的使用方式如下:

复制代码
1
record struct Point(int X, int Y);

.NET6中也支持record来声明一个基于classrecord,这和原来的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。例如​ ​readonly 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}"; } }

这里需要注意defaultnew的差别,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优化内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是酷炫冬天最近收集整理的关于.NET6新特新 struct优化的全部内容,更多相关内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(162)

评论列表共有 0 条评论

立即
投稿
返回
顶部