Consider the following enumeration:
[System.Flags]
public enum EnumType: int
{
None = 0,
Black = 2,
White = 4,
Both = Black | White,
Either = ???, // How would you do this?
}
Currently, I have written an extension method:
public static bool IsEither (this EnumType type)
{
return
(
((type & EnumType.Major) == EnumType.Major)
|| ((type & EnumType.Minor) == EnumType.Minor)
);
}
Is there a more elegant way to achieve this?
UPDATE: As evident from the answers, EnumType.Either has no place inside the enum itself.
With flags enums, an “any of” check can be generalised to
(value & mask) != 0, so this is:assuming you fix the fact that:
(as
Black & Whiteis an error, this is zero)For completeness, an “all of” check can be generalised to
(value & mask) == mask.