.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();
相关推荐
武藤一雄20 小时前
WPF深度解析Behavior
windows·c#·.net·wpf·.netcore
weixin_443478511 天前
Flutter学习之输入组件
学习·flutter·servlet
不超限1 天前
.net core项目部署至信创环境
.netcore
程序0071 天前
在线五子棋小游戏(.NET Core+FreeSql+WebSocket ) html+js
websocket·html·.netcore
武藤一雄2 天前
C# 设计模式大全(第一弹|7种)
microsoft·设计模式·微软·c#·.net·.netcore
滴滴答答哒2 天前
.netcore集成CAP事件总线,消息队列
.netcore
时光追逐者2 天前
一个基于 .NET Core + Vue3 构建的开源全栈平台 Admin 系统
开源·c#·.net·.netcore·admin系统
武藤一雄3 天前
C#:nameof 运算符全指南
开发语言·microsoft·c#·.net·.netcore
武藤一雄3 天前
C# 核心技术解析:Parse vs TryParse 实战指南
开发语言·windows·microsoft·微软·c#·.netcore
happymaker06263 天前
servlet、jsp、请求转发、重定向的一些个人理解
java·开发语言·servlet