Is there a way to add an “All values” option to an enum without having to change its value every time a new value is added to the enum?
[Flags]
public enum SomeEnum
{
SomeValue = 1,
SomeValue2 = 1 << 1,
SomeValue3 = 1 << 2,
SomeValue4 = 1 << 3,
All = ?
}
Update:
Ended up inheriting from long and using long.MaxValue for All option.
Since you should define the empty value in a
Flags enum such asNone = 0, the simplest way of defining theAllvalue is by simply inverting all the bits inNone`.Note that
~0instead of~Nonewill not work for unsigned backing types as that is -1, which is not a valid value for unsigned.Edit: Answer was modified to use an inverted None instead of an explicit constant such as 0x7FFFFFFF or ~0, as this also works for unsigned