How to generically combine all bits (I mean OR all values) of a [FLAGS] enum, having only valid values (bits) declared in the enum?
Ex: [Flags] enum SuitsFlags { Spades = 1, Clubs = 2, Diamonds = 4, Hearts = 8 } => Expected solution would: return Spades | Clubs | Diamonds | Hearts; but using a generic function.
I try to ask the question previously but I asked the wrong question: Enum fun... Why these 2 errors.... My question was closed but peoples gave me some great clues on how to do it. I finally did it with pretty much works (trial and errors). So I wanted to leave a trace of my solution to help others, and potentially have better solution from other users too.
It is not the first time I need that. Although anybody can do it quickly by OR (val1 | val2 | ...) each values manually, that could lead to runtime error when a new enum value is added and the programmer forget to verify each and every places where the enum is used.
This is my second answer which I prefer because of its advantages:
Usage sample:
Code: