目录
什么是record?
record是.NET 5中的一种新特性,可以看作是一种概念上不可变的类。records可以帮助我们在C#中更容易地处理数据,同时提供了重要的功能,如对象相等性、hashcode和解构。
与类不同,records具有值语义。也就是说,当比较两个records的实例时,比较的是这些实例的属性而非引用。这意味着,如果两个records的属性值相同,它们就是相等的。
record也可以简化需要类似于Dto的数据结构容器的定义。
使用record
Person p1 = new("小明", "南山", "[email protected]"); Person p2 = new("小明", "南山", "[email protected]"); Console.WriteLine(p1 == p2); public record Person(string Name, string Address, string Email);
像定义一个类一样,public class Person,只是将class关键字替换成record关键字。然后属性是用括号来定义。
默认的record声明是class,如果想声明一个struct
public record struct Person(string Name, string Address, string Email);
record是不可变的类型,括号中声明的属性在构造之后不可变更。可以使用==按属性的值进行比较。可以直接作为hash的key以及结构。
record可以像普通类一样扩展可变更的属性和自定义的方法,语法如下
public record Person(string Name, string Address, string Email) { public required string PhoneNumber { get; set; } public static IEnumerable<Person> GetAll() { yield return new Person("张三", "123 Main St", "[email protected]") { PhoneNumber = "123456789"}; yield return new Person("李四", "456 Elm St", "[email protected]") { PhoneNumber = "123456789" }; yield return new Person("王二", "789 Oak St", "[email protected]") { PhoneNumber = "123456789" }; ; } public string GetDisplayName() => $"{Name} ({Email})"; };
record解构
record可以通过解构,将对象解构为元组,方便一次性获取record中的属性值,
Person p1 = new("小明", "南山", "[email protected]") ; var (name,address,email) = p1 ;
record原理
record的原理是编译器提供支持,上述Person定义反编译结果如下
public class Person : IEquatable<Person> { [CompilerGenerated] protected virtual Type EqualityContract { [CompilerGenerated] get { return typeof(Person); } } public string Name { get; set/*init*/; } public string Address { get; set/*init*/; } public string Email { get; set/*init*/; } public Person(string Name, string Address, string Email) { this.Name = Name; this.Address = Address; this.Email = Email; base..ctor(); } [CompilerGenerated] public override string ToString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("Person"); stringBuilder.Append(" { "); if (PrintMembers(stringBuilder)) { stringBuilder.Append(' '); } stringBuilder.Append('}'); return stringBuilder.ToString(); } [CompilerGenerated] protected virtual bool PrintMembers(StringBuilder builder) { RuntimeHelpers.EnsureSufficientExecutionStack(); builder.Append("Name = "); builder.Append((object?)Name); builder.Append(", Address = "); builder.Append((object?)Address); builder.Append(", Email = "); builder.Append((object?)Email); return true; } [CompilerGenerated] public static bool operator !=(Person? left, Person? right) { return !(left == right); } [CompilerGenerated] public static bool operator ==(Person? left, Person? right) { return (object)left == right || (left?.Equals(right) ?? false); } [CompilerGenerated] public override int GetHashCode() { return ((EqualityComparer<Type>.Default.GetHashCode(EqualityContract) * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name)) * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Address)) * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Email); } [CompilerGenerated] public override bool Equals(object? obj) { return Equals(obj as Person); } [CompilerGenerated] public virtual bool Equals(Person? other) { return (object)this == other || ((object)other != null && EqualityContract == other.EqualityContract && EqualityComparer<string>.Default.Equals(Name, other.Name) && EqualityComparer<string>.Default.Equals(Address, other.Address) && EqualityComparer<string>.Default.Equals(Email, other.Email)); } [CompilerGenerated] protected Person(Person original) { Name = original.Name; Address = original.Address; Email = original.Email; } [CompilerGenerated] public void Deconstruct(out string Name, out string Address, out string Email) { Name = this.Name; Address = this.Address; Email = this.Email; } }
可以看到,编译器给使用了record关键字的定义生成了对应的属性和构造函数,并且重写了ToString(),GetHashCode,Equals还有一个解构函数和!=和==运算符。其实看到这里就明白了,为什么record可以提供值比较,解构,hash等。
不可变性是因为record的属性是使用了init关键字而不是set,这样子如果对record的对象属性赋值,编译器会报错。
值相等性是重定义了!=和==运算符
hash是因为重写了GetHashCode,Equals
解构是定义了Deconstruct方法
结论
我们介绍了.NET 5中引入的record类型及其优点。但对于许多数据对象的简单情况,如值对象和DTO,推荐使用record类型。虽然record可以定义可变更的属性和添加方法,不过这样子有点违背了record的初衷。