马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
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();
复制代码
|