.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();
相关推荐
编程小Y21 小时前
Servlet 与 Tomcat 白话全解析:从核心原理到实战部署
java·servlet·tomcat
程序员阿鹏1 天前
tomcat正常启动但 SpringMVC 控制器无法启动
java·spring·servlet·tomcat·maven·intellij-idea
爱宇阳1 天前
Jenkins 多语言 Agent 完整部署教程
运维·servlet·jenkins
武藤一雄1 天前
.NET中到底什么是SignalR (持续更新)
后端·微软·c#·asp.net·.net·.netcore·signalr
by__csdn1 天前
第二章 (.NET Core环境搭建)第二节( Visual Studio Code)
ide·vscode·c#·vue·asp.net·.net·.netcore
by__csdn1 天前
第二章 (.NET Core环境搭建)第三节( Visual Studio for Mac)
ide·kubernetes·c#·asp.net·.net·.netcore·visual studio
避避风港2 天前
深入解析Tomcat工作流程与Servlet体系
java·servlet·tomcat
weixin_307779132 天前
高效通知:Jenkins邮件插件完全指南与进阶配置
java·servlet·jenkins
青衫码上行2 天前
【JavaWeb学习 | 第20篇】EL表达式与JSTL标签
java·学习·servlet·java-ee
武藤一雄2 天前
C#:进程/线程/多线程/AppDomain详解
后端·微软·c#·asp.net·.net·wpf·.netcore