.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-1=03 天前
C# 数据结构和特殊类型
数据结构·c#·.net·.netcore
露天赏雪6 天前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
1-1=06 天前
C# IO和序列化
c#·.net·.netcore
宝桥南山7 天前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
1-1=08 天前
C# 特性(Attribute)
c#·.net·.netcore
淡海水9 天前
10-01-互操作-C#与C++边界的内存管理
unity·c#·游戏引擎·.netcore·gc
1-1=09 天前
C# 反射
c#·.net·.netcore
cakeism8259 天前
2026 中国企业 DevOps 平台选型:从工具链到一体化平台的决策框架
运维·servlet·devops
1-1=010 天前
C# 泛型
c#·.net·.netcore
瑞码空间11 天前
JSP与Servlet核心开发与CRUD实战笔记
java·笔记·servlet·jsp