2020年11月12日星期四

.Net5,C#9 新语法(逻辑和属性模式,记录)

 

 代码:

namespace ConsoleApp1
{

class Program
{
static void Main(string[] args)
{
//创建list数组,=号右边可省略
List<student>? ts = new();
var StuA = new student("明明子", "1606", 18);
//直接复制更新StuA并赋值给StuB
var StuB = StuA with { Name = "BaiPiaoD" };
ts.Add(StuA);
ts.Add(StuB);
//根据属性判断
if (ts[0] is student or { Name: "明明子" } or { Age: >= 12 })
{
Console.WriteLine("或许捕捉到一只成年明明子");
}
//根据属性判断
if (ts[1] is { Age: > 12 and < 23, Name: "BaiPiaoD" })
{
Console.WriteLine(ts[1].Name + GoodBoy(ts[1].Age));
}
//Switch语法,判断参数age,返回不同的值
string GoodBoy(int age) => age switch
{
< 5 => "崽崽",
< 18 => "小屁孩",
< 50 => "猛男",
_ => "老家伙"
};
}

}
//C#9包含一种称为记录的新类型的类record。与常规类相比,它有许多优点,其中一半与更简洁的语法有关。
public record student(string Name, string Class, int Age);
}

 

原文转载:http://www.shaoqun.com/a/489867.html

sca:https://www.ikjzd.com/w/2424

西农:https://www.ikjzd.com/w/1368

网上1号店:https://www.ikjzd.com/w/2263


代码:namespaceConsoleApp1{classProgram{staticvoidMain(string[]args){//创建list数组,=号右边可省略List<student>?ts=new();varStuA=newstudent("明明子","1606",18);//直接复制更新StuA并赋值给StuBvarStuB=Stu
picitup:https://www.ikjzd.com/w/446
西集网:https://www.ikjzd.com/w/1353
网传的亚马逊listing主图新规?是真是假?:https://www.ikjzd.com/home/124831
新加坡圣淘沙的玩法 :http://tour.shaoqun.com/a/12325.html
李小龙乐园地址?顺德李小龙乐园自驾游路线?:http://tour.shaoqun.com/a/59930.html

没有评论:

发表评论