切换语句 - 变量"case"?

dot*_*ner 2 .net c# switch-statement

因为ScoreOption,我希望获得以下输入"A","B"和T_(状态),例如T_NY

如何为第三个选项T_(state)编写case switch语句?

switch(ScoreOption.ToUpper().Trim())
{
    case "A":
        ....
        break;
    case "B":
        ....
        break;
    case T_????
        ....
        break;
}
Run Code Online (Sandbox Code Playgroud)

我不妨写一下if-else语句?

And*_*rey 16

string s = ScoreOption.ToUpper().Trim();
switch(s)
{
    case "A":

        ....

        break;
    case "B":

        ....

        break;
    default:
        if (s.StartsWith("T_"))
        {
        ....
        }                       
        break;

}
Run Code Online (Sandbox Code Playgroud)


kem*_*002 10

在switch语句中,不能将变量作为大小写.你必须要做类似的事情

case "T_NY":
case "T_OH":
break;
Run Code Online (Sandbox Code Playgroud)

等等

现在你能做的是

switch (ScoreOption.ToUpper().Trim())
{
   case "A":
    break;
   case "B":
    break;
   default: 
//catch all the T_ items here. provided that you have specifed all other 
//scenarios above the default option.
    break;

}
Run Code Online (Sandbox Code Playgroud)