一.switch case when
8.0新特性,把贼好用!(熟悉T-SQL的会感觉很亲切,新的switch扩展变得和T-SQL的Case When Then End类似,可以支持不等式区间的布尔表达式) ,其可读性吊锤if else!
class Person
{
public int grade { get; set; }
public string name { get; set; }
public string level { get; set; }
}
IList<Person> persons= new List<Person>()
{
new Person{ grade = 50, name = "A_People", level = "None" },
new Person{ grade = 70, name = "B_People", level = "None" },
new Person{ grade = 91, name = "C_People", level = "None" }
};
foreach (Person p in persons)
{
switch (p)
{
case Person obj when obj.grade < 60:
{
obj.level = "不及格";
break;
}
case Person obj when obj.grade > 60 && obj.grade <= 70:
{
obj.level = "及格";
break;
}
case Person obj when obj.grade > 60 && obj.grade <= 70:
{
obj.level = "良好";
break;
}
case Person obj when obj.grade >= 90:
{
obj.level = "优秀";
break;
}
default:
{
Person.level = "None";
break;
}
}
Console.WriteLine($"此人的:{p.name} 成绩:{p.level}");