如何检查多个案例并根据该案例进行分配?
我有:
str[1] = (Setting.DBL(this.fieldTxt3.Tag.ToString()) == 1000 ? Setting.IP2 : Setting.IP4);
str[3] = (Setting.DBL(this.fieldTxt3.Tag.ToString()) == 1000 ? "TBSS2" : "TBSS4");
Run Code Online (Sandbox Code Playgroud)
但是我想要更像的东西:
str[1] = (Setting.DBL(this.fieldTxt3.Tag.ToString()) >= 3000 ? Setting.IP5 : Setting.IP4 : <additional options>);
str[3] = (Setting.DBL(this.fieldTxt3.Tag.ToString()) == 1000 ? "TBSS2" : "TBSS4" : <additional options>);
Run Code Online (Sandbox Code Playgroud)
您想要的功能称为"匹配表达式",尚未添加到C#中.它可能会在未来的版本中.有关详细信息,请参阅roslyn github论坛.
建议语法类似于:
double area = someShape switch (
case Line line: 0,
case Rectangle r: r.Width * r.Height,
case Circle c: Math.PI * c.Radius * c.Radius,
case *: throw new ApplicationException()
)
Run Code Online (Sandbox Code Playgroud)
我们在这里说"切换someShape;如果它是一条线,它的区域是零......"等等.
在此之前,请使用if语句.