开发者

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

开发者 https://www.devze.com 2023-01-18 14:40 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Multiple Cases in Switch:
This question already has answers here: Closed 12 years ago.

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;
   }


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;  
}  
0

精彩评论

暂无评论...
验证码 换一张
取 消