.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 天前
【黑马点评日记02】Redis缓存优化:商户查询性能提升百倍
java·spring boot·spring·servlet·tomcat·intellij-idea
m0_744724931 天前
Servlet原理
servlet
lifewange1 天前
Jenkins Windows MSI 安装包完整安装教程
windows·servlet·jenkins
MoFe11 天前
【.net core】【RabbitMq】rabbitmq在.net core中的简单使用
分布式·rabbitmq·.netcore
我登哥MVP1 天前
【SpringMVC笔记】 - 6 - RESTFul编程风格
java·spring boot·spring·servlet·tomcat·maven·restful
van久2 天前
Day15:EF Core 入门 + CodeFirst(就业核心版)
.netcore
叫我黎大侠2 天前
.NET 实战:调用千问视觉模型实现 OCR(车票识别完整教程)
阿里云·ai·c#·ocr·asp.net·.net·.netcore
van久2 天前
Day15-4:【日志】中间件和过滤器 的对比选择
.netcore
我登哥MVP2 天前
【SpringMVC笔记】 - 5 - View
java·spring boot·spring·servlet·tomcat·maven·intellij-idea
我登哥MVP2 天前
【SpringMVC笔记】 - 4 - 三个域对象
java·spring boot·spring·servlet·tomcat·maven·intellij-idea