Neatest way to 'OR' all values in a Flagged Enum?
c#, enums
Solution
If it makes sense to have an `All` member, just provide it directly:
[Flags]
public enum mytest
{
a = 1,
b = 2,
c = 4,
All = 7
}
Though, a more idiomatic way to write these could be:
[Flags]
public enum MyTest
{
A = 1,
B = 1 << 0x01,
C = 1 << 0x02,
All = A | B | C
}
This shows the logical progression of the enum values, and in the `All` case, makes it easy to add another member.
Problem
Given the `enum`: ``` [Flags] public enum mytest { a = 1, b = 2, c = 4 } ``` I've come up with two ways to represent all values in a single variable: ``` var OR1 = (mytest)Enum.GetNames(typeof(mytest)).Sum(a => (int)Enum.Parse(typeof(mytest), a)); var OR2 = (mytest)(typeof(mytest).GetEnumValues() as mytest[]).Sum(a => (int)a); ``` Now, although they both work, is there a neater way? Possibly a .NET method I'm missing? Edit: For clarification, I need the function to be dynamic - I don't want to calculate it by specifying every single `enum` value.