If Enum Has specific flag, check to see if it has any others

I have a rather large Flag enum called AmendmentType. I need to check to see that if it has specific Enums and any others.

For Example:

var foo = AmendmentType.Item1;

if (foo.HasFlag(AmendmentType.Item1) && (ANYTHING ELSE))
{
//DO NOT ALLOW
}
else if (foo.HasFlag(AmendmentType.Item2) && foo.HasFlag(AmendmentType.Item6))
{
//DO NOT ALLOW
}
else
{
//ALLOW
}

How would this be possible? There are about 20 different items in the Flag Enum and it seems like there should be an easier way than checking all possible combinations.

Jon Skeet
people
quotationmark

If you're only interested in the part you've labeled as "anything else", you can use:

if (foo.HasFlag(AmendmentTypeEnum.Item1) && (foo & ~AmendmentTypeEnum.Item1) != 0)

Or just check that it isn't exactly equal to Item1:

if (foo.HasFlag(AmendmentTypeEnum.Item1) && foo != AmendmentTypeEnum.Item1)

Note that checking for the presence of all of multiple flags only needs a single HasFlag call too:

else if (foo.HasFlag(AmendmentTypeEnum.Item2 | AmendmentTypeEnum.Item6))

(I'd also suggest removing the Enum suffix - it'll be a lot easier to read the code without it :)

people

See more on this question at Stackoverflow