如何检查是否设置了标志组合的任何标志? [英] How to check if any flags of a flag combination are set?
本文介绍了如何检查是否设置了标志组合的任何标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这个枚举:
[Flags]
enum Letters
{
A = 1,
B = 2,
C = 4,
AB = A | B,
All = A | B | C,
}
要检查例如 AB
是否设置,我可以这样做:
To check if for example AB
is set I can do this:
if((letter & Letters.AB) == Letters.AB)
是否有比以下更简单的方法来检查是否设置了组合标志常量的任何标志?
Is there a simpler way to check if any of the flags of a combined flag constant are set than the following?
if((letter & Letters.A) == Letters.A || (letter & Letters.B) == Letters.B)
例如,可以将 &
与某些东西交换吗?
Could one for example swap the &
with something?
推荐答案
如果您想知道 letter 是否包含 AB 中的任何字母,您必须使用 AND &代码> 运算符.类似的东西:
If you want to know if letter has any of the letters in AB you must use the AND &
operator. Something like:
if ((letter & Letters.AB) != 0)
{
// Some flag (A,B or both) is enabled
}
else
{
// None of them are enabled
}
这篇关于如何检查是否设置了标志组合的任何标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文