.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();
相关推荐
cly11 小时前
Jenkins重置管理员密码
运维·servlet·jenkins
weixin_421994781 天前
认识数据 - 变量与数据类型
c#·.net·.netcore
老龄程序员1 天前
记一次由于.netcore程序堆栈溢出的问题分析
.netcore
m0_748252381 天前
Servlet 数据库访问
数据库·servlet
派大鑫wink1 天前
【Day33】Servlet 基础:生命周期、Request/Response 对象实战
java·servlet
栗子叶2 天前
Spring 中 Servlet 容器和 Python FastAPI 对比
python·spring·servlet·fastapi
一然明月2 天前
.NET Core基础
.netcore
亲爱的非洲野猪3 天前
SpringBoot启动流程深度剖析:从@SpringBootApplication到Servlet容器就绪
hive·spring boot·servlet
AIGCExplore3 天前
Jenkins 配置 Publish over SSH 自动部署项目
servlet·ssh·jenkins
Java 码农3 天前
jenkins + gitlab 构建自由风格的任务
servlet·gitlab·jenkins