Say I have an enum:
[Flags]
public enum Alpha
{
NULL = 0,
A0 = 1,
A1 = 2,
A2 = 4,
A3 = 8
}
And I have the following:
Alpha selected = A0 | A1 | A2
Alpha compare = A1 | A3
I want to check if any of the selected
flags are also in the compare
flags.
The only way I can think to do this so far is (pseudocode)
foreach(Alpha flag in Enum.GetValues(typeof(Alpha)))
{
if (selected.HasFlag(flag) && compare.HasFlag(flag))
{
return true
}
}
return false
Is there a more logic-al way to do this?
Because it is treated as bit field, you can use it in the same fashion.
Indicates that an enumeration can be treated as a bit field; that is, a set of flags
bool result = (selected & compare) > 0; // First solution
bool result2 = (selected & compare).Any(); // Second solution