Is there a way to traverse multiple case statements without declaring case value: duplicate?
I know this works:
switch (value) { case 1: case 2: case 3: //do some stuff break; case 4: case 5: case 6: //do some different stuff break; default: //default stuff break; }
But I want to do something like this:
switch (value) { case 1,2,3: //Do Something break; case 4,5,6: //Do Something break; default: //Do the Default break; }
Do I consider using this grammar in other languages?Or did I miss something?
#1st floor
// @Jennifer Owens: Absolutely correct, the following code doesn't work:
case 1 | 3 | 5: //not working do something
The only way is:
case 1: case 2: case 3: // do something break;
The code you're looking for can be run on Visual Basic, and you can easily put scopes within it...in any of the options on the switch, or if it's not convenient, I recommend using Visual Basic to make.dll in extreme cases and re-import it into your C# project.
Note: In Visual Basic, the equivalent switch is the selection case.
#2nd floor
I think this has already been answered.However, I think you can still grammatically better mix these two options by:
switch (value) { case 1: case 2: case 3: // Do Something break; case 4: case 5: case 6: // Do Something break; default: // Do Something break; }
#3rd floor
The initial question was a bit late, but I posted this answer so that anyone who wants to use a newer version (provided by default in C_7-Visual Studio 2017 / the.NET Framework 4.6.2) will find it helpful.
In C#7, you can now use switch Statements Make range-based switching, which will help solve the OP problem.
Example:
int i = 5; switch (i) { case int n when (n >= 7): Console.WriteLine($"I am 7 or above: {n}"); break; case int n when (n >= 4 && n <= 6 ): Console.WriteLine($"I am between 4 and 6: {n}"); break; case int n when (n <= 3): Console.WriteLine($"I am 3 or less: {n}"); break; } // Output: I am between 4 and 6: 5
Notes:
- Parentheses (and) are not required in the when condition, but are used in this example to highlight the comparison.
- You can also use VaR instead of int.For example: case var n when n >= 7:.
#4th floor
If you have a large number of strings (or any other type) that are all doing the same thing, I recommend using a combination of the string List and string.Contains properties.
So if you have a very large switch statement, for example:
switch (stringValue) { case "cat": case "dog": case "string3": ... case "+1000 more string": //Too many string to write a case for all! //Do something; case "a lonely case" //Do something else; . . . }
You may want to replace it with the following if statement:
//Define all the similar "case" string in a List List<string> listString = new List<string>(){ "cat", "dog", "string3", "+1000 more string"}; //Use string.Contains to find what you are looking for if (listString.Contains(stringValue)) { //Do something; } else { //Then go back to a switch statement inside the else for the remaining cases if you really need to }
This scaling is good for any number of string cases.
#5th floor
This is the complete C#7 solution...
switch (value) { case var s when new[] { 1,2,3 }.Contains(s): //Do Something break; case var s when new[] { 4,5,6 }.Contains(s): //Do Something break; default: //Do the Default break; }
Strings can also be used...
switch (mystring) { case var s when new[] { "Alpha","Beta","Gamma" }.Contains(s): //Do Something break; ... }