I’m using a enum with flags attribute as a way of tracking status.
An example is the following:
Created = 1
Completed = 2
Dispatched = 4
Without writing anything too rigid (if check this, do that, if check that, do this) i want to be able to find the highest flag which has been set so in this example:
Item.Status = Status.Created | Status.Completed
the mythical method would return 2 – as completed is the flag set with the highest value.
GetMaxSetFlagValue(Item.Status) // returns 2
I’ve found questions which revolved around the actual enum, just not a value which uses flags. I’m fairly sure this could be achieved with Linq…?
Something like the following should work:
The method will fail if T is not an enum type, so a check should preferably be performed in the beginning of the method. Also it will not work for an enum with an underlying type larger than
int(i.e.long).