集团网站建设费用,美工个人网站,广告行业做网站哪个好,网站维护中是什么意思c#编码技巧(十四)#xff1a;新语法糖record深入分析 
从 C# 9 开始新增了一个关键字record#xff0c;用于封装数据。 record实质是微软提供的一个语法糖#xff0c;因很多开源项目都用到了这个关键字#xff0c;说明这个语法糖比较实用。 那么这个record类型和普通class类…c#编码技巧(十四)新语法糖record深入分析 
从 C# 9 开始新增了一个关键字record用于封装数据。 record实质是微软提供的一个语法糖因很多开源项目都用到了这个关键字说明这个语法糖比较实用。 那么这个record类型和普通class类型有什么区别呢我们可以通过工具探究一下源码 首先新建一个普通的类PersonCls添加两个属性 /// summary/// 普通类/// /summarypublic class PersonCls{public string Name { get; set; }public string Address { get; set; }}再建一个record类型PersonRecordrecord类型的声明简洁一行就搞定其中属性名字放在括号里编译器自动为我们生成两个属性 /// summary/// record类型/// /summary/// param nameName/param/// param nameAge/parampublic record PersonRecord(string Name, int Age);//也即public record class PersonRecord(string Name, int Age);//其中class可省略var person  new PersonRecord(Tom, 18);利用反编译工具查看普通类PersonCls代码果然非常普通除了两属性什么都没有 
using System;
using System.Runtime.CompilerServices;namespace DotNet6
{// Token: 0x02000005 RID: 5[NullableContext(1)][Nullable(0)]public class PersonCls{// Token: 0x17000001 RID: 1// (get) Token: 0x06000005 RID: 5 RVA: 0x00002092 File Offset: 0x00000292// (set) Token: 0x06000006 RID: 6 RVA: 0x0000209A File Offset: 0x0000029Apublic string Name { get; set; }// Token: 0x17000002 RID: 2// (get) Token: 0x06000007 RID: 7 RVA: 0x000020A3 File Offset: 0x000002A3// (set) Token: 0x06000008 RID: 8 RVA: 0x000020AB File Offset: 0x000002ABpublic string Address { get; set; }}
}同样利用反编译工具查看PersonRecord却生成了很多代码 其中 
包含一个有参构造函数生成了两个属性生成了ToString和运算符比较、Equals比较的方法还有其他一些方法方法都很简单暂不做分析 
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;namespace DotNet6
{// Token: 0x02000006 RID: 6[NullableContext(1)][Nullable(0)]public class PersonRecord : IEquatablePersonRecord{// Token: 0x0600000A RID: 10 RVA: 0x000020BD File Offset: 0x000002BDpublic PersonRecord(string Name, string Address){this.Name  Name;this.Address  Address;base..ctor();}// Token: 0x17000003 RID: 3// (get) Token: 0x0600000B RID: 11 RVA: 0x000020D4 File Offset: 0x000002D4[CompilerGenerated]protected virtual Type EqualityContract{[CompilerGenerated]get{return typeof(PersonRecord);}}// Token: 0x17000004 RID: 4// (get) Token: 0x0600000C RID: 12 RVA: 0x000020E0 File Offset: 0x000002E0// (set) Token: 0x0600000D RID: 13 RVA: 0x000020E8 File Offset: 0x000002E8public string Name { get; set; }// Token: 0x17000005 RID: 5// (get) Token: 0x0600000E RID: 14 RVA: 0x000020F1 File Offset: 0x000002F1// (set) Token: 0x0600000F RID: 15 RVA: 0x000020F9 File Offset: 0x000002F9public string Address { get; set; }// Token: 0x06000010 RID: 16 RVA: 0x00002104 File Offset: 0x00000304[CompilerGenerated]public override string ToString(){StringBuilder stringBuilder  new StringBuilder();stringBuilder.Append(PersonRecord);stringBuilder.Append( { );if (this.PrintMembers(stringBuilder)){stringBuilder.Append( );}stringBuilder.Append(});return stringBuilder.ToString();}// Token: 0x06000011 RID: 17 RVA: 0x00002150 File Offset: 0x00000350[CompilerGenerated]protected virtual bool PrintMembers(StringBuilder builder){RuntimeHelpers.EnsureSufficientExecutionStack();builder.Append(Name  );builder.Append(this.Name);builder.Append(, Address  );builder.Append(this.Address);return true;}// Token: 0x06000012 RID: 18 RVA: 0x0000218A File Offset: 0x0000038A[NullableContext(2)][CompilerGenerated]public static bool operator !(PersonRecord left, PersonRecord right){return !(left  right);}// Token: 0x06000013 RID: 19 RVA: 0x00002196 File Offset: 0x00000396[NullableContext(2)][CompilerGenerated]public static bool operator (PersonRecord left, PersonRecord right){return left  right || (left ! null  left.Equals(right));}// Token: 0x06000014 RID: 20 RVA: 0x000021AC File Offset: 0x000003AC[CompilerGenerated]public override int GetHashCode(){return (EqualityComparerType.Default.GetHashCode(this.EqualityContract) * -1521134295  EqualityComparerstring.Default.GetHashCode(this.Namek__BackingField)) * -1521134295  EqualityComparerstring.Default.GetHashCode(this.Addressk__BackingField);}// Token: 0x06000015 RID: 21 RVA: 0x000021EC File Offset: 0x000003EC[NullableContext(2)][CompilerGenerated]public override bool Equals(object obj){return this.Equals(obj as PersonRecord);}// Token: 0x06000016 RID: 22 RVA: 0x000021FC File Offset: 0x000003FC[NullableContext(2)][CompilerGenerated]public virtual bool Equals(PersonRecord other){return this  other || (other ! null  this.EqualityContract  other.EqualityContract  EqualityComparerstring.Default.Equals(this.Namek__BackingField, other.Namek__BackingField)  EqualityComparerstring.Default.Equals(this.Addressk__BackingField, other.Addressk__BackingField));}// Token: 0x06000018 RID: 24 RVA: 0x0000225F File Offset: 0x0000045F[CompilerGenerated]protected PersonRecord(PersonRecord original){this.Name  original.Namek__BackingField;this.Address  original.Addressk__BackingField;}// Token: 0x06000019 RID: 25 RVA: 0x00002280 File Offset: 0x00000480[CompilerGenerated]public void Deconstruct(out string Name, out string Address){Name  this.Name;Address  this.Address;}}
} 
使用这些方法可以看到与普通class类不同的是 
record的ToString()可以输出值属性值相同的两个record类型使用或Equals比较判断为相等 static async Task Main(string[] args){//record类型ToString()可以输出名称值而类只会输出命名空间类名var clsStr  (new PersonCls() { Name  LiLei, AddressGD }.ToString());var str  (new PersonRecord(LiLei, GD)).ToString();//输出PersonRecord { Name  LiLei, Address  GD }//属性值相同的普通类使用或Equals比较判断为不相等var cls1  new PersonCls() { Name  Tom, Address  CN };var cls2  new PersonCls() { Name  Tom, Address  CN };bool isClassOperatorEquals  cls1  cls2;//falsebool isClassEquals  cls1.Equals(cls2);//falsebool isClassReferenceEquls  ReferenceEquals(cls1, cls2);//false//属性值相同的两个record类型使用或Equals比较判断为相等使用ReferenceEquals比较判断为不相等因为引用是确实是不同var rcd1  new PersonRecord(Ben, HK);var rcd2  new PersonRecord(Ben, HK);bool isRecordOperatorEquals  rcd1  rcd2;// true因为值相同bool isRecordEquals  rcd1.Equals(rcd2);//true因为值相同bool isRecordReferenceEquls  ReferenceEquals(rcd1, rcd2);//false:引用不同//rcd1.Address  shenzhen;//定义在括号内的record的属性值在new之后不能修改//想要能修改的属性就像Level那样声明/*public record PersonRecordCanChange(string Name, string Address){public string Level { get; set; }}*/}record还有其他用法比如使用with { }可以复制这个record var rcdCopy  rcd1 with { };//复制Console.WriteLine($name{rcdCopy.Name}, address{rcdCopy.Address});//nameBen, addressHK只想部分复制可以在{}内更改部分属性的值 var rcdCopy  rcd1 with { };//复制Console.WriteLine($name{rcdCopy.Name}, address{rcdCopy.Address});//nameBen, addressBeijing可以继承 public record Animal(string Head, string Body);public record Human(string Head, string Body, string Hand): Animal(Head, Body);如果确实需要自定义属性可以这样写 public record Points{//声明属性X,Y并在构造函数中注入赋值public Points(double time, double distance)  (X, Y)  (time, distance);public double X { get; set; }public double Y { get; set; }}var point  new Points(time: 10, distance: 990);var x  point.X;var y  point.Y;但不建议这样做这样违背了record的设计初衷 
综上所述 
record实质是微软提供的一个语法糖本质就是一个类这个类里生成了若干个方法这些方法就使得record与类区别开来record的写法简单快捷可用在数据传输对象中如dto等能够大大提升效率简化代码 文章转载自: http://www.morning.dbbcq.cn.gov.cn.dbbcq.cn http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.zwyuan.com.gov.cn.zwyuan.com http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.fslrx.cn.gov.cn.fslrx.cn http://www.morning.qrhh.cn.gov.cn.qrhh.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.youyouling.cn.gov.cn.youyouling.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.yccnj.cn.gov.cn.yccnj.cn http://www.morning.zwwhq.cn.gov.cn.zwwhq.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.rgxn.cn.gov.cn.rgxn.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.wtyqs.cn.gov.cn.wtyqs.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.xjmpg.cn.gov.cn.xjmpg.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.nlywq.cn.gov.cn.nlywq.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.kpxky.cn.gov.cn.kpxky.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.lqlc.cn.gov.cn.lqlc.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.frpb.cn.gov.cn.frpb.cn http://www.morning.ywqsk.cn.gov.cn.ywqsk.cn http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.bmfqg.cn.gov.cn.bmfqg.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.iterlog.com.gov.cn.iterlog.com http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn http://www.morning.ttxnj.cn.gov.cn.ttxnj.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.rdkgw.cn.gov.cn.rdkgw.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.ggcjf.cn.gov.cn.ggcjf.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.bbtn.cn.gov.cn.bbtn.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.nslwj.cn.gov.cn.nslwj.cn http://www.morning.hnrdtz.com.gov.cn.hnrdtz.com http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn