I have defined an enum
with the [Flag]
attribute.
Given an integer value, I would like to print all the enum
values for the bits set in the integer value.
Here's what I have so far.
string s = string.Join(", ", Enum.GetValues(typeof(MyEnumType)).OfType<Enum>()
.Where(x => (MyIntValue & (int)x) != 0));
But the typecast to int
in the last line gives me the following error.
Cannot convert type 'System.Enum' to 'int'
I wish Microsoft would replace all the Framework code that returns Array
in favor of types that support LINQ.
But is there an easy way to do this?