How to handle multiple values inside one case?
如何处理一个
这条路对吗?
1 2 3 4 5 6 7 8 9 10 11 | switch(text) { case"first option": { } case"second option": { string a="first or Second"; break; } } |
在文档中称为"多个标签",可以在msdn的c文档中找到。
A switch statement can include any number of switch sections, and each section can have one or more case labels (as shown in the string case labels example below). However, no two case labels may contain the same constant value.
您更改的代码:
1 2 3 4 5 6 7 8 9 10 11 | string a = null; switch(text) { case"first option": case"second option": { a ="first or Second"; break; } } |
注意,我把
这是可能的
1 2 3 4 5 6 7 8 9 10 | switch(i) { case 4: case 5: case 6: { //do someting break; } } |
如果您希望能够将两者视为不同的情况,那么最好只使用
1 2 3 4 5 6 7 8 9 10 11 12 | if (first && second) { Console.WriteLine("first and second"); } else if (first) { Console.WriteLine("first only"); } else if (second) { Console.WriteLine("second only"); } |