C# 9 中的 recordide
C# 9 新特性 — record 解讀C# 9 中引入了 record,record 是一個特殊類,用它來實現 model 在有些狀況下會很是的好用測試
record RecordPerson { public string Name { get; init; } public int Age { get; init; } } record RecordPerson2(string Name, int Age); public static void MainTest() { var p1 = new RecordPerson() { Name = "Tom", Age = 12, }; Console.WriteLine(p1); var p2 = p1 with { Age = 10 }; Console.WriteLine(p2); var p3 = new RecordPerson() { Name = "Tom", Age = 12 }; Console.WriteLine(p3); Console.WriteLine($"p1 Equals p3 =:{p1 == p3}"); RecordPerson2 p4 = new("Tom", 12); Console.WriteLine(p4); }
這裏的示例,用 record 聲明瞭兩個 model,第二個 model 聲明的時候使用了簡化的寫法,ui
record RecordPerson2(string Name, int Age); 這樣的聲明意味着,構造方法有兩個參數,分別是 string Name 和 int Age,並對應着兩個屬性,屬性的聲明方式和 RecordPerson 同樣 public string Name { get; init; } 都是一個 get 一個 initthis
對於 record 支持一個 with 表達式,來修改某幾個屬性的值,這對於有不少屬性都相同的場景來講是及其方便的,來看一下上面示例的輸出結果調試
那麼 record 內部發生了什麼呢,咱們來反編譯看一下,咱們看一下使用 DnSpy 反編譯的結果orm
RecordPerson對象
private class RecordPerson : IEquatable{ // Token: 0x17000007 RID: 7 // (get) Token: 0x06000027 RID: 39 RVA: 0x000025F4 File Offset: 0x000007F4 [Nullable(1)] protected virtual Type EqualityContract { [NullableContext(1)] [CompilerGenerated] get { return typeof(RecordSample.RecordPerson); } } // Token: 0x17000008 RID: 8 // (get) Token: 0x06000028 RID: 40 RVA: 0x00002600 File Offset: 0x00000800 // (set) Token: 0x06000029 RID: 41 RVA: 0x00002608 File Offset: 0x00000808 public string Name { [CompilerGenerated] get { return this.k__BackingField; } [CompilerGenerated] set { this.k__BackingField = value; } } // Token: 0x17000009 RID: 9 // (get) Token: 0x0600002A RID: 42 RVA: 0x00002611 File Offset: 0x00000811 // (set) Token: 0x0600002B RID: 43 RVA: 0x00002619 File Offset: 0x00000819 public int Age { [CompilerGenerated] get { return this.k__BackingField; } [CompilerGenerated] set { this.k__BackingField = value; } } // Token: 0x0600002C RID: 44 RVA: 0x00002624 File Offset: 0x00000824 public override string ToString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("RecordPerson"); stringBuilder.Append(" { "); if (this.PrintMembers(stringBuilder)) { stringBuilder.Append(" "); } stringBuilder.Append("}"); return stringBuilder.ToString(); } // Token: 0x0600002D RID: 45 RVA: 0x00002678 File Offset: 0x00000878 [NullableContext(1)] protected virtual bool PrintMembers(StringBuilder builder) { builder.Append("Name"); builder.Append(" = "); builder.Append(this.Name); builder.Append(", "); builder.Append("Age"); builder.Append(" = "); builder.Append(this.Age.ToString()); return true; } // Token: 0x0600002E RID: 46 RVA: 0x000026EA File Offset: 0x000008EA [NullableContext(2)] public static bool operator !=(RecordSample.RecordPerson r1, RecordSample.RecordPerson r2) { return !(r1 == r2); } // Token: 0x0600002F RID: 47 RVA: 0x000026F6 File Offset: 0x000008F6 [NullableContext(2)] public static bool operator ==(RecordSample.RecordPerson r1, RecordSample.RecordPerson r2) { return r1 == r2 || (r1 != null && r1.Equals(r2)); } // Token: 0x06000030 RID: 48 RVA: 0x0000270C File Offset: 0x0000090C public override int GetHashCode() { return (EqualityComparer.Default.GetHashCode(this.EqualityContract) * -1521134295 + EqualityComparer.Default.GetHashCode(this.k__BackingField)) * -1521134295 + EqualityComparer.Default.GetHashCode(this.k__BackingField); } // Token: 0x06000031 RID: 49 RVA: 0x0000274C File Offset: 0x0000094C [NullableContext(2)] public override bool Equals(object obj) { return this.Equals(obj as RecordSample.RecordPerson); } // Token: 0x06000032 RID: 50 RVA: 0x0000275C File Offset: 0x0000095C [NullableContext(2)] public virtual bool Equals(RecordSample.RecordPerson other) { return other != null && this.EqualityContract == other.EqualityContract && EqualityComparer.Default.Equals(this.k__BackingField, other.k__BackingField) && EqualityComparer.Default.Equals(this.k__BackingField, other.k__BackingField); } // Token: 0x06000033 RID: 51 RVA: 0x000027B0 File Offset: 0x000009B0 [NullableContext(1)] public virtual RecordSample.RecordPerson$() { return new RecordSample.RecordPerson(this); } // Token: 0x06000034 RID: 52 RVA: 0x000027B8 File Offset: 0x000009B8 protected RecordPerson([Nullable(1)] RecordSample.RecordPerson original) { this.Name = original.k__BackingField; this.Age = original.k__BackingField; } // Token: 0x06000035 RID: 53 RVA: 0x000027D9 File Offset: 0x000009D9 public RecordPerson() { } // Token: 0x0400000C RID: 12 [CompilerGenerated] [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly stringk__BackingField; // Token: 0x0400000D RID: 13 [CompilerGenerated] [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly intk__BackingField; }
從上面的反編譯結果能夠看的出來,record 其實就是一個 class,只是編譯器會幫咱們作一些事情,編譯器幫咱們作了哪些事呢?blog
再來看一下 RecordPerson2get
private class RecordPerson2 : IEquatable{ // Token: 0x06000036 RID: 54 RVA: 0x000027E2 File Offset: 0x000009E2 public RecordPerson2(string Name, int Age) { this.Name = Name; this.Age = Age; base..ctor(); } // Token: 0x1700000A RID: 10 // (get) Token: 0x06000037 RID: 55 RVA: 0x000027F9 File Offset: 0x000009F9 [Nullable(1)] protected virtual Type EqualityContract { [NullableContext(1)] [CompilerGenerated] get { return typeof(RecordSample.RecordPerson2); } } // Token: 0x1700000B RID: 11 // (get) Token: 0x06000038 RID: 56 RVA: 0x00002805 File Offset: 0x00000A05 // (set) Token: 0x06000039 RID: 57 RVA: 0x0000280D File Offset: 0x00000A0D public string Name { [CompilerGenerated] get { return this.k__BackingField; } [CompilerGenerated] set { this.k__BackingField = value; } } // Token: 0x1700000C RID: 12 // (get) Token: 0x0600003A RID: 58 RVA: 0x00002816 File Offset: 0x00000A16 // (set) Token: 0x0600003B RID: 59 RVA: 0x0000281E File Offset: 0x00000A1E public int Age { [CompilerGenerated] get { return this.k__BackingField; } [CompilerGenerated] set { this.k__BackingField = value; } } // Token: 0x0600003C RID: 60 RVA: 0x00002828 File Offset: 0x00000A28 public override string ToString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("RecordPerson2"); stringBuilder.Append(" { "); if (this.PrintMembers(stringBuilder)) { stringBuilder.Append(" "); } stringBuilder.Append("}"); return stringBuilder.ToString(); } // Token: 0x0600003D RID: 61 RVA: 0x0000287C File Offset: 0x00000A7C [NullableContext(1)] protected virtual bool PrintMembers(StringBuilder builder) { builder.Append("Name"); builder.Append(" = "); builder.Append(this.Name); builder.Append(", "); builder.Append("Age"); builder.Append(" = "); builder.Append(this.Age.ToString()); return true; } // Token: 0x0600003E RID: 62 RVA: 0x000028EE File Offset: 0x00000AEE [NullableContext(2)] public static bool operator !=(RecordSample.RecordPerson2 r1, RecordSample.RecordPerson2 r2) { return !(r1 == r2); } // Token: 0x0600003F RID: 63 RVA: 0x000028FA File Offset: 0x00000AFA [NullableContext(2)] public static bool operator ==(RecordSample.RecordPerson2 r1, RecordSample.RecordPerson2 r2) { return r1 == r2 || (r1 != null && r1.Equals(r2)); } // Token: 0x06000040 RID: 64 RVA: 0x00002910 File Offset: 0x00000B10 public override int GetHashCode() { return (EqualityComparer.Default.GetHashCode(this.EqualityContract) * -1521134295 + EqualityComparer.Default.GetHashCode(this.k__BackingField)) * -1521134295 + EqualityComparer.Default.GetHashCode(this.k__BackingField); } // Token: 0x06000041 RID: 65 RVA: 0x00002950 File Offset: 0x00000B50 [NullableContext(2)] public override bool Equals(object obj) { return this.Equals(obj as RecordSample.RecordPerson2); } // Token: 0x06000042 RID: 66 RVA: 0x00002960 File Offset: 0x00000B60 [NullableContext(2)] public virtual bool Equals(RecordSample.RecordPerson2 other) { return other != null && this.EqualityContract == other.EqualityContract && EqualityComparer.Default.Equals(this.k__BackingField, other.k__BackingField) && EqualityComparer.Default.Equals(this.k__BackingField, other.k__BackingField); } // Token: 0x06000043 RID: 67 RVA: 0x000029B4 File Offset: 0x00000BB4 [NullableContext(1)] public virtual RecordSample.RecordPerson2$() { return new RecordSample.RecordPerson2(this); } // Token: 0x06000044 RID: 68 RVA: 0x000029BC File Offset: 0x00000BBC protected RecordPerson2([Nullable(1)] RecordSample.RecordPerson2 original) { this.Name = original.k__BackingField; this.Age = original.k__BackingField; } // Token: 0x06000045 RID: 69 RVA: 0x000029DD File Offset: 0x00000BDD public void Deconstruct(out string Name, out int Age) { Name = this.Name; Age = this.Age; } // Token: 0x0400000E RID: 14 [CompilerGenerated] [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly stringk__BackingField; // Token: 0x0400000F RID: 15 [CompilerGenerated] [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly intk__BackingField; }
RecordPerson2 相比 RecordPerson 的區別在於構造器不一樣:原型
看上面反編譯的結果,能夠看出:
foreach (var (name, age) in new[] { p4 }) { Console.WriteLine($"{name}={age}"); }
再來看一下測試方法的反編譯結果:
RecordSample.RecordPerson p = new RecordSample.RecordPerson { Name = "Tom", Age = 12 }; Console.WriteLine(p); RecordSample.RecordPerson recordPerson = p.$(); recordPerson.Age = 10; RecordSample.RecordPerson p2 = recordPerson; Console.WriteLine(p2); RecordSample.RecordPerson p3 = new RecordSample.RecordPerson { Name = "Tom", Age = 12 }; Console.WriteLine(p3); Console.WriteLine(string.Format("p1 Equals p3 =:{0}", p == p3)); RecordSample.RecordPerson2 p4 = new RecordSample.RecordPerson2("Tom", 12); Console.WriteLine(p4);
這裏主要能夠看到 with 表達式的實現,其實就是調用 方法複製了一個對象,並修改指定的屬性值
record 實現了基於值的相等性比較,而且實現了 原型模式,能夠比較方便的建立一個新的值徹底相等的對象,這對於有一些業務場景來講是很是適合使用 record 來代替原來的實現的