有没有办法做到这一点?
int i = object.GetString() switch
{
"this" => 1,
"that" => 2,
"the other" => 3,
_ => someMethod([switch value])
}在switch表达式中使用打开的值?或者我必须这么做
var myString = object.GetString()
int i = myString switch
{
"this" => 1,
"that" => 2,
"the other" => 3,
_ => someMethod(myString)
}我知道声明myString没什么大不了的;我只是想知道语法是否存在。
发布于 2021-02-11 10:12:54
这个怎么样?
int i = object.GetString() switch
{
"this" => 1,
"that" => 2,
"the other" => 3,
{ } s => someMethod(s)
}它会得到除了null之外的任何东西。
当然,只有当你想在那里捕获任何类型时,它才是有用的。如果您确定它将是一个string值,并且someMethod也需要一个string值,那么您可以这样做:
string s => someMethod(s)发布于 2021-02-11 10:12:57
是的,这很简单:
int i = object.GetString() switch
{
"this" => 1,
"that" => 2,
"the other" => 3,
string value => someMethod(value)
};https://stackoverflow.com/questions/66147790
复制相似问题