Hello, I have just began learning flags, and I wanted to know how to check if set of Flags A has any contents of Flags B.
Example
[Flags]
public enum FlagCategory
{
None = 0,
A = 1 << 0,
B = 1 << 1,
C = 1 << 2,
All = ~0
}
public bool HasAnyFlag(FlagCategory flagA, FlagCategory flagB)
{
if (/*if set flagA has any flags of set flagB*/)
{
return true;
}
return false;
}
Thank you.
Solved with looping through each selected enum, and checking if flags.HasFlags(activeFlag).
bool doHave = false;
// Loop through entire available enum values of FlagCategory.
foreach (FlagCategory f in Enum.GetValues(typeof(FlagCategory)))
{
// If flag is selected
if (otherFlagCategory.HasFlag(f))
{
// If flags has a selected flag from otherFlagCategory
if (_flags.Category.HasFlag(f))
{
doHave = true;
}
}
}
return doHave;
But if there is a better and more elegant/magical way, please do post here.
Thank you.