12

Given the enum:

[Flags]
enum foo
{
a = 1,
b = 2,
c = 4
}

then

foo example = a | b;

If I don't know if foo contains c, previously I have been writing the following

if (example & foo.c == foo.c)
    example  = example ^ foo.c;

Is there a way to do this without checking for the existance of foo.c in example?

As when it comes to additions, I can just do an OR, and if the enum value already exists in example then it doesnt matter.

4

2 回答 2

26

我想你想要:

example &= ~foo.c;

换句话说,执行一个按位“与”掩码,设置for 之外的每个位c

编辑:我应该在某个时候向Unconstrained Melody添加一个“例外” ,所以你可以写:

example = example.Except(foo.c);

如果您对此感兴趣,请告诉我,我会看看周末我能做些什么……

于 2011-09-23T08:46:26.643 回答
9

和它与 foo.c 的补充:

example = example & ~foo.c
于 2011-09-23T08:47:50.940 回答