繁体   English   中英

如何检查两个以上的值<condition> ?<if true> :<if false> ?

[英]How can I check more than two values with <condition>?<if true>:<if false>?

如何检查多个案例并基于此进行分配?

我有:

str[1] = (Setting.DBL(this.fieldTxt3.Tag.ToString()) == 1000 ? Setting.IP2 : Setting.IP4);
str[3] = (Setting.DBL(this.fieldTxt3.Tag.ToString()) == 1000 ? "TBSS2" : "TBSS4");

但我想要更像:

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>);

您想要的功能称为“匹配表达式”,它尚未添加到 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()
)

我们在这里所说的“打开 someShape;如果它是一条线,它的面积为零......”等等。

在此之前,请使用if语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM