I have the following enum:
[Flags]
public enum Permissions
{
None = 0x0000,
All = 0xFFFF
}
If either None or All are raised, no other flag should be raised.
How do I check if either None or All are raised and nothing else?
I have the following enum:
[Flags]
public enum Permissions
{
None = 0x0000,
All = 0xFFFF
}
If either None or All are raised, no other flag should be raised.
How do I check if either None or All are raised and nothing else?
In a flags enum, None
should be zero, and All
should be the cumulative bitwise sum. This makes the maths pretty easy, then:
if(value == Permissions.None || value == Permissions.All) {...}
maybe written as a switch
if you prefer...
However, in the general case, you can test for a complete flags match (against any number of bits) with:
if((value & wanted) == wanted) {...}
and to test for any overlap (i.e. any common bits - wanted
needs to be non-zero):
if((value & wanted) != 0) {...}
if(value|Permissions.None)==Permissions.None;
This can check Permissions.None is raised. The rest can be done in the same manner.