实体:
public record Student1(int Id, string Name);
//public record class Student 全写
public record Student //简写
{
public Student(int id, string name)
{
Id = id;
Name = name;
}
public int Id { get; init; }
public string Name { get; init; }
/// <summary>
/// 重写ToString
/// </summary>
/// <returns></returns>
public override string ToString()
{
return System.Text.Json.JsonSerializer.Serialize(this); ;
}
/// <summary>
/// 解构函数
/// </summary>
/// <returns></returns>
public void Deconstruct(out int Id, out string Name)
{
Name = this.Name;
Id = this.Id;
}
}
program:
static void Main(string[] args)
{
Student stu = new(1, "czj");
Console.WriteLine("stu:" + stu.ToString());
var stu1 = stu with { };
Console.WriteLine($"stu equal stu1 : {stu.Equals(stu1)}");
Console.WriteLine($"stu == stu1 : {stu == stu1}");
var stu2 = stu with { Name = "zzx" };
Console.WriteLine("stu2:" + stu2.ToString());
(int id, string name1) = stu2;
Console.WriteLine($"stu2: Id = {id} Name = {name1}");
Console.ReadLine();
}