Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use truct Optimization in NET6

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要为大家展示了"NET6中truct优化怎么用",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"NET6中truct优化怎么用"这篇文章吧。

一、Record Struct

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

它的使用方式如下:

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声明有参数构造器,则会生成一个隐式的无参构造。

代码如下:

var p1 = new Point(1, 2);var p2 = p with { X = 2 };Console.WriteLine(p1);Console.WriteLine(p2);Console.WriteLine(new Point());

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

上述代码输出如下:

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);

属性的声明是这样的:

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支持用户自定义无参构造方法,我们可以在无参构造方法中加入初始化逻辑,

代码如下如下:

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来修改部分属性

代码如下:

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中truct优化怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report