c#特性

//不可修改的记录类
public record Person(string name);

//可修改的记录类
public record struct Person(string name);

//带内属性的
public record struct Person(string name)
{
     public List<string> Nums { get; init; } = default!;
}
//调用使用, 使用with赋值
Person p1
= new("aaa") { Nums = new List<string>() { "1", "2" } }; Person p2 = p1 with { name = "bbbb" };

 

// lambda表达式
var choose = object (bool b) => b ? 1 : "two";
var result = choose(false); // result: two
// 常量模式
static string GetLabel(int source) => source switch
{
      1 => "one",
      2 or 3 =>"two",
      >= 4 and <=6 =>"more",
      _=>"other", //相当于 default:
};
GetLabel(1);  // one

 

//多返回值
public static (int a, string b) GetTest(string str)
{
     if (str is not (null or ""))
         return (0, "");
     return (123, "name");
}

 

posted @ 2022-06-20 19:16  第一序列  阅读(15)  评论(0编辑  收藏  举报