简体   繁体   English

'||' 在switch语句中的两个枚举之间使用的运算符

[英]'||' operators to be used between two enums in a switch statement

I want to check if a function returns either of two enum values, each of the same enumeration type. 我想检查一个函数是否返回两个枚举值之一,每个枚举值都相同。

For simplicity's sake, I attempted to create a case as follows: 为了简单起见,我尝试创建一个案例,如下所示:

case EnumerationTypeExample.TypeA || EnumerationTypeExample.TypeB:

Unfortunately, this does not please C#, which says I cannot use an '||' 不幸的是,这不能取悦C#,它说我不能使用'||' operator despite them being of the same type; 尽管它们是相同类型的运算符; strange. 奇怪。 Might there be a way this could be done otherwise? 可能有其他方法可以做到吗? An if statement perhaps might work and I may retreat to that, however, I would much rather use a switch statement if possible. if语句也许可行,我可能会撤退,但是,如果可能的话,我宁愿使用switch语句。

A case statement must be constants, not a computation. case语句必须是常量,而不是计算。 However you can use fall through in this case: 但是fall through在这种情况下,您可以使用fall through

switch (something)
{
    case EnumerationTypeExample.TypeA:
    case EnumerationTypeExample.TypeB:
    {
        DoSomething();   
        break;  
    }
}

Now the code will run in both situations. 现在,代码将在两种情况下运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM