dotnet record
作者:互联网
本质上record是一个实现了Equals()/GetHashCode()和ToString()的class
record Person(int Id, string Name, int Age) // 这3个都是只读属性
{
public string NickName { get; set; } // 可写属性
void Play()
{
Console.WriteLine($"ppp:{Name}");
}
}
//
Person p1 = new Person(1, "p1", 11) { NickName = "n1" };
Person p2 = new Person(1, "p1", 11) { NickName = "n2" };
// false,因为NickName也参与比较
Console.WriteLine(p1 == p2);
// 使用with语法,可以复制record,再改一些属性。
Person p3 = p1 with { };
Console.WriteLine(p1 == p3);
Person p4 = p1 with { Age = 12 };
Console.WriteLine(p1 == p4);
标签:p1,Console,Person,record,WriteLine,dotnet,NickName 来源: https://www.cnblogs.com/mryux/p/15863855.html