简体   繁体   中英

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

How can I check more than one case and assign based on that?

I have:

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

But I want something more like:

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

The feature you want is called "match expressions" and it has not been added to C# yet. It likely will be in a future version. See the roslyn github forum for details.

The syntax is proposed to be something like:

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

Where we are saying here "switch on someShape; if it is a line, its area is zero..." and so on.

Until that happens, use if statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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