目录
一、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。例如 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}";
}
}
|
这里需要注意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优化内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://www.tuicool.com/articles/aEBJBfv
相关文章
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-06-04 77
-
如何在Magento电子商务网站中设置PayPal支付方式?
2025-05-25 42 -
2025-05-27 87
-
2025-06-04 45
-
PHP将MySQL的查询结果转换为数组并用where拼接的示例
2025-05-29 77

