.Net Core Record 类型

public class Person {

public string id {get;init;}

public string code {get;init;}

public string name {get;init;}

}

//Person 属性不可单独赋值,相当于使用record定义

public record Person string id,string code,string name)

//record类型定义对象

1、定义的属性只能在初始化时赋值

2、重写了Equals等对象类型的比较方法,在两个不同引用的record对象的内容相同时,对两者进行==比较,判断两者相等为true

3、重写了ToString()方法,便于输出属性内容。还重写了GetHashCode()Equals()方法。

复制代码
public record   User(string code,string name);

User u1 = new User("101", "yxs");
User u2 = new User("101", "yxs");

//with的使用
//with表达式,用于拷贝原有对象,并对特定属性进行修改
User u3 = u2 with {code="", name = "name2"};


Console.WriteLine(u1.ToString());//输出对象值
Console.WriteLine(u1.Equals(u2));//true
Console.ReadLine();
相关推荐
罗马苏丹默罕默德1 天前
Ubuntu下部署.NetCore WebApi的方法
数据库·ubuntu·.netcore
ZePingPingZe1 天前
SpringMVC与Servlet容器[Tomcat]
spring boot·servlet·tomcat
座山雕~2 天前
servlet
servlet
二哈喇子!2 天前
JavaSE 与 JavaEE 知识点整合
java·servlet·tomcat
lingxiao168883 天前
WebApi详解+Unity注入--中篇:.net core的WebAPI
unity·c#·.netcore
老龄程序员3 天前
基于OpenIddict6.4.0搭建的授权UI管理界面
.netcore
武藤一雄4 天前
C# 关于多线程如何实现需要注意的问题(持续更新)
windows·后端·microsoft·c#·.net·.netcore·死锁
冰茶_4 天前
WPF路由事件:隧道与冒泡机制解析
学习·c#·.net·wpf·.netcore·mvvm
武藤一雄4 天前
C# 关于GC垃圾回收需要注意的问题(持续更新)
后端·微软·c#·.net·.netcore
武藤一雄4 天前
C# 关于应用程序域(AppDomain)需要注意的问题(持续更新)
后端·microsoft·微软·c#·.net·.netcore