开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜