/// <summary> /// 基于 .Net 5 /// </summary> namespace CSharp9._0 { public class FooInfo { public int FooId { get; init; } public string FooName { get; init; } } public record FoodInfo { public int FoodId { get; init; } public string FoodName { get; init; } } }
namespace CSharp9._0 { class Program { static void Main(string[] args) { //记录 init FooInfo foo = new FooInfo() { FooId = 1, FooName = "张三" }; //X foo.FooName = ""; //record FoodInfo foodInfo = new FoodInfo() { FoodId = 2, FoodName = "李四" }; //X foodInfo.FoodName = "王五"; 属性是set时可以使用 //with 配套record使用 //X var foo1 = foo with { } //with 表达式指示编译器创建记录副本 var food = foodInfo with { FoodId = 2 }; //简化 FooInfo foo1 = new();//语法糖 //为null判断 string name = string.Empty; if (name is not null) { name = "王五"; } } } } //顶级语句,注释上面 //Console.WriteLine("12");