C#9引入的自定义类型record
生活随笔
收集整理的這篇文章主要介紹了
C#9引入的自定义类型record
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
record是C#9引入的新的引用類型,詳細見官方文檔:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-9#record-types
在C#中,引用類型有:interface,class,delegate,數組;現在record加入了進來,這是一個與類極度相似,但又不同的類型,重點在比較兩個對象相等時,不是用引用地址作比較(雖然它是個引用類型),而是用“類型名{屬性名1=屬性值,屬性名2=屬性值,……}”(注1:這是一種形象但不嚴格的說法)。
DDD中,有實體和值對象的概念,其中值對象定義如下:
通過對象屬性值來識別的對象,它將多個相關屬性組合為一個概念整體——《實現領域驅動設計》
就是通過這個對象的各個屬性相同不相同來判斷是不是同一個值對象,這時,record就最合適不過了,它能直接判斷兩個實例化后的對象是否相等,。
record本質上個class,對反射,dapper的適配,面向對象的特征都是相同的,demo見:https://github.com/axzxs2001/Asp.NetCoreExperiment/blob/master/Asp.NetCoreExperiment/CSharp/RecordTypeDemo/Program.cs
using Dapper; using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Reflection;namespace RecordTypeDemo {class Program{static void Main(string[] args){//接口類型IShow show = new Entity1() { ID = 1, Name = "桂素偉" };Console.WriteLine(show);//輸出結果:Entity1 { ID = 1, Name = 桂素偉 }//抽象類型Entity entity = new Entity1() { ID = 1, Name = "桂素偉" };Console.WriteLine(entity);//輸出結果:Entity1 { ID = 1, Name = 桂素偉 }//注1:雖然 類型名{屬性名1=屬性值,屬性名2=屬性值,……} 相等,但比較結果是不等的Console.WriteLine($"show == entity結果:{show == entity}");//輸出結果:show == entity結果::False//實體類型var entity1 = new Entity1() { ID = 1, Name = "桂素偉" };Console.WriteLine(entity1);//輸出結果:Entity1 { ID = 1, Name = 桂素偉 }Console.WriteLine($"(entity1 == entity結果:{entity1 == entity}");//輸出結果:(entity1 == entity結果:TrueConsole.WriteLine($"(entity1 == entity結果:{entity1.Equals(entity)}");//輸出結果:(entity1 == entity結果:TrueAddEntity(entity);ReflectionTest(entity1);}/// <summary>/// 反射中使用record,和類相山/// </summary>/// <param name="entity"></param>static void ReflectionTest(Entity entity){var type = Assembly.GetExecutingAssembly().GetType("RecordTypeDemo.Entity");Console.WriteLine(type.IsClass);}/// <summary>/// 對dapper適配/// </summary>/// <param name="entity"></param>/// <returns></returns>static bool AddEntity(Entity entity){using (var con = new NpgsqlConnection("Server=127.0.0.1;Port=5432;UserId=postgres;Password=postgres2018;Database=postgres;")){var list = con.Query<Entity1>("select * from entitys").ToList();if (!list.Contains(entity)){con.Execute("insert into entitys(id,name) values(@id,@name)", entity);}return true;}/*表結構 CREATE TABLE public.entitys (id integer NOT NULL DEFAULT nextval('entitys_id_seq'::regclass),name character varying(256) COLLATE pg_catalog."default",CONSTRAINT entitys_pkey PRIMARY KEY (id) ) */}}#region 面向對象特征和類一樣/// <summary>/// 接口/// </summary>public interface IShow{void Show();}/// <summary>/// 抽象記錄/// </summary>public abstract record Entity : IShow{public abstract int ID { get; set; }public abstract string Name { get; set; }public void Show(){Console.WriteLine($"{this.GetType().Name}:");Console.WriteLine($"{this.ToString()}");}}/// <summary>/// 記錄/// </summary>public record Entity1 : Entity{public override int ID{get;set;}public override string Name{get;set;}}#endregion }總結
以上是生活随笔為你收集整理的C#9引入的自定义类型record的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从读大学到工作,我的这几年时光是如何度过
- 下一篇: C# 8: 默认接口方法