.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();
相关推荐
weixin_421994782 天前
基于 .NET 9.0 的高性能轻量级令牌桶限流服务
.net·.netcore·令牌桶
weixin_421994782 天前
MVC 模式初探
mvc·.net·.netcore
hzc09876543213 天前
报错The default superclass, “jakarta.servlet.http.HttpServlet“(已经配置好tomcat)
http·servlet·tomcat
weixin_421994784 天前
互联网与 Web 应用简介
.net·.netcore
该叫啥5 天前
Spring Bean 生命周期
java·spring·servlet
全栈小56 天前
【C#】合理使用DeepSeek相关AI应用为我们提供强有力的开发工具,在.net core 6.0框架下使用JsonNode动态解析json字符串,如何正确使用单问号和双问号做好空值处理
人工智能·c#·json·.netcore·deepseek
未定义.2216 天前
第2篇:请求实战!覆盖GET/POST/请求头/参数全场景
java·python·http·servlet·自动化·jenkins
Eloudy6 天前
CUTLASS C++ 快速入门指南
开发语言·c++·servlet
时光追逐者7 天前
C#/.NET/.NET Core优秀项目和框架2026年1月简报
c#·.net·.netcore
大尚来也9 天前
双库协同,各取所长:.NET Core 中 PostgreSQL 与 SQLite 的优雅融合实战
postgresql·sqlite·.netcore