C# 9 中引入了 record
,record
是一個特殊類,用它來實現 model 在有些狀況下會很是的好用git
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 聲明的時候使用了簡化的寫法,github
record RecordPerson2(string Name, int Age);
這樣的聲明意味着,構造方法有兩個參數,分別是 string Name
和 int Age
,並對應着兩個屬性,屬性的聲明方式和 RecordPerson
同樣 public string Name { get; init; }
都是一個 get
一個 init
ide
對於 record
支持一個 with
表達式,來修改某幾個屬性的值,這對於有不少屬性都相同的場景來講是及其方便的,來看一下上面示例的輸出結果測試
那麼 record
內部發生了什麼呢,咱們來反編譯看一下,咱們看一下使用 DnSpy
反編譯的結果ui
RecordPerson
this
private class RecordPerson : IEquatable<RecordSample.RecordPerson> { // 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.<Name>k__BackingField; } [CompilerGenerated] set { this.<Name>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.<Age>k__BackingField; } [CompilerGenerated] set { this.<Age>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<Type>.Default.GetHashCode(this.EqualityContract) * -1521134295 + EqualityComparer<string>.Default.GetHashCode(this.<Name>k__BackingField)) * -1521134295 + EqualityComparer<int>.Default.GetHashCode(this.<Age>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<string>.Default.Equals(this.<Name>k__BackingField, other.<Name>k__BackingField) && EqualityComparer<int>.Default.Equals(this.<Age>k__BackingField, other.<Age>k__BackingField); } // Token: 0x06000033 RID: 51 RVA: 0x000027B0 File Offset: 0x000009B0 [NullableContext(1)] public virtual RecordSample.RecordPerson <Clone>$() { return new RecordSample.RecordPerson(this); } // Token: 0x06000034 RID: 52 RVA: 0x000027B8 File Offset: 0x000009B8 protected RecordPerson([Nullable(1)] RecordSample.RecordPerson original) { this.Name = original.<Name>k__BackingField; this.Age = original.<Age>k__BackingField; } // Token: 0x06000035 RID: 53 RVA: 0x000027D9 File Offset: 0x000009D9 public RecordPerson() { } // Token: 0x0400000C RID: 12 [CompilerGenerated] [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly string <Name>k__BackingField; // Token: 0x0400000D RID: 13 [CompilerGenerated] [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly int <Age>k__BackingField; }
從上面的反編譯結果能夠看的出來,record
其實就是一個 class
,只是編譯器會幫咱們作一些事情,編譯器幫咱們作了哪些事呢?調試
==
/!=
operator 和 GetHashCode
ToString
方法,也提供了 PrintMembers
方法來實現比較方便只顯示某些比較重要的參數EqualityContract
方法來指定類型比較的類型,默認是當前類型<Clone>$
方法和一個特殊的構造方法,用來克隆一個對象,至關於 record
幫咱們實現了一個淺複製 的 原型模式,仍是強類型的,這個方法在代碼裏不能直接調用,當咱們使用 with
表達式的時候,編譯器會調用這個方法,並對某些屬性進行賦值再來看一下 RecordPerson2
code
private class RecordPerson2 : IEquatable<RecordSample.RecordPerson2> { // 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.<Name>k__BackingField; } [CompilerGenerated] set { this.<Name>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.<Age>k__BackingField; } [CompilerGenerated] set { this.<Age>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<Type>.Default.GetHashCode(this.EqualityContract) * -1521134295 + EqualityComparer<string>.Default.GetHashCode(this.<Name>k__BackingField)) * -1521134295 + EqualityComparer<int>.Default.GetHashCode(this.<Age>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<string>.Default.Equals(this.<Name>k__BackingField, other.<Name>k__BackingField) && EqualityComparer<int>.Default.Equals(this.<Age>k__BackingField, other.<Age>k__BackingField); } // Token: 0x06000043 RID: 67 RVA: 0x000029B4 File Offset: 0x00000BB4 [NullableContext(1)] public virtual RecordSample.RecordPerson2 <Clone>$() { return new RecordSample.RecordPerson2(this); } // Token: 0x06000044 RID: 68 RVA: 0x000029BC File Offset: 0x00000BBC protected RecordPerson2([Nullable(1)] RecordSample.RecordPerson2 original) { this.Name = original.<Name>k__BackingField; this.Age = original.<Age>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 string <Name>k__BackingField; // Token: 0x0400000F RID: 15 [CompilerGenerated] [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly int <Age>k__BackingField; }
RecordPerson2
相比 RecordPerson
的區別在於構造器不一樣:orm
看上面反編譯的結果,能夠看出:對象
RecordPeron2
和 RecordPerson
都聲明瞭兩個屬性,都是 public string Name { get; init; }
/public int Age { get; init; }
RecordPerson
的構造方法是無參構造方法,而 RecordPerson2
的構造方法是 RecordPerson2(string Name, int Age)
Deconstruct
方法,使得咱們能夠比較方便的解析一個對象的值,能夠參考下面這個示例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.<Clone>$(); 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
表達式的實現,其實就是調用 <Clone>$
方法複製了一個對象,並修改指定的屬性值
record
實現了基於值的相等性比較,而且實現了 原型模式,能夠比較方便的建立一個新的值徹底相等的對象,這對於有一些業務場景來講是很是適合使用 record
來代替原來的實現的