I know that using multiple values in a switch case should mean you ought to be using an if-else setup but this is more of a theory question than a practicality.

My question is thus:

switch(x){
    case 4:
        break;
    case 5 || 6:
        break;
}

The above 5 || 6 illustrates the case firing on either x being 5 or 6.

Is this at all possible?

Edit: This is a question as to the design of the switch decision making rather than using fall-throughs.

2 Answers

2
hessam hedieh On Best Solutions

Of course you can, with help of Pattern matching in C#7
I know this not the elegant way, but its what OP asks for

switch(x){
    case var rule when x == 4:
        //do your things
        break;
    case var rule when x == 5 || x == 6:
        //do your other things
        break;
}
9
Owen Pauling On

You can just fall through like this to match 5 or 6:

switch(x){
    case 4:
        break;
    case 5:
    case 6:
        // do something for 5 or 6
        break;
}