.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 天前
从静态参数到动态复用:Jenkins Parameters、Active Choices 与 Scriptler 实战指南
java·servlet·jenkins·devops
1-1=03 天前
ASP.NET Core 自定义容器 和 自定义日志
asp.net·.netcore
1-1=05 天前
ASP.NET Core HTTP 请求管道
.netcore
飞奔的瓜牛9 天前
Ansible 实战:典型使用场景、Playbook 编写与 Python 调用接口详解
python·servlet·ansible
宝桥南山12 天前
Visual Studio - 记录一个Visual Studio 2026(Insiders)的更新问题
windows·microsoft·微软·.net·.netcore·visual studio
1-1=01 个月前
C# 数据结构和特殊类型
数据结构·c#·.net·.netcore
露天赏雪1 个月前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
1-1=01 个月前
C# IO和序列化
c#·.net·.netcore
宝桥南山1 个月前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
1-1=01 个月前
C# 特性(Attribute)
c#·.net·.netcore