This question already has answers here:
Value is in enum list
                                
                                    (6个答案)
                                
                        
                                2年前关闭。
            
                    
这是示例,CommandType是一个枚举。这是一个无标志枚举。

CommandType cmdType = CommandType.back;
if (cmdType == CommandType.back || cmdType == CommandType.forward || cmdType == CommandType.previous || cmdType == CommandType.home)
{
//do something
}


有什么方法可以简化“ if”语句吗?

最佳答案

switch怎么样?

switch(cmdType)
{
    case CommandType.back:
    case CommandType.forward:
    case CommandType.previous:
    case CommandType.home:
       // do something
       break;
}

07-24 09:44
查看更多