Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch statement with multiple constant-expression in c#. Is it possible? [duplicate]

Possible Duplicate:
Multiple Cases in Switch:

Is it possible to do a multiple constant-expression switch statement like

switch (i) {
   case "run","notrun", "runfaster": //Something like this.
      DoRun();
      break;
   case "save":
      DoSave();
      break;
   default:
      InvalidCommand(command);
      break;
   }
like image 997
Amra Avatar asked Oct 01 '10 13:10

Amra


People also ask

Can switch-case have multiple conditions in C?

A switch statement—or simply a case statement—is a control flow mechanism that determines the execution of a program based on the value of a variable or an expression. Using a switch statement allows you to test multiple conditions and only execute a specific block if the condition is true.

Can a switch-case have multiple values?

Switch Case The switch cases must be unique constant values. It can be bool, char, string, integer, enum, or corresponding nullable type.

Which constants can be used in switch statement?

constant1 , constant2 and so on following the case keywords must be of integer type (like int , long int etc ) or character type. It can also be an expression which yields an integer value. Each case statement must have only one constant.


1 Answers

Yes, it is. You can use multiple case labels for the same section:

switch (i)  {       case "run":      case "notrun":     case "runfaster":            DoRun();           break;       case "save":           DoSave();           break;       default:           InvalidCommand(command);           break;   }   
like image 167
D'Arcy Rittich Avatar answered Sep 28 '22 05:09

D'Arcy Rittich