2021年2月10日星期三

Can you get the value being switched on inside a switch expression

Is there a way to do this

int i = object.GetString() switch  {      "this" => 1,      "that" => 2,      "the other" => 3,      _ => someMethod([switch value])  }    

to use the value being switched on inside the switch expression? Or do I have to do this

var myString = object.GetString()  int i = myString switch  {      "this" => 1,      "that" => 2,      "the other" => 3,      _ => someMethod(myString)  }    

I know it's not that big of a deal to declare myString; I'm just wondering if the syntax exists.

https://stackoverflow.com/questions/66147790/can-you-get-the-value-being-switched-on-inside-a-switch-expression February 11, 2021 at 10:04AM

没有评论:

发表评论