EnumExtensions Class
Definition
Provides non-boxing bitwise helpers for flag-style enumerations - membership tests and add, remove, and toggle operations that work uniformly across every enum underlying type.
public static class EnumExtensions
- Inheritance
-
EnumExtensions
- Inherited Members
Remarks
The built-in HasFlag(Enum) boxes both operands on every call, and the bitwise
operators (|, &, ^, ~) are unavailable on an open generic enum type. This class
supplies generic, allocation-free equivalents constrained to struct, System.Enum.
Each operation reinterprets the enum value as its underlying integer, applies the bitwise operation, and - for the mutating helpers - reinterprets the result back to the enum type. No boxing, reflection, or Convert call is involved, so the helpers are safe to use on hot paths.
These helpers are intended for enumerations annotated with FlagsAttribute; they operate purely on the bit pattern and do not validate that the supplied values correspond to declared members.
Methods
ClearFlag<TEnum>(TEnum, TEnum)
Returns a copy of value with every bit in flags cleared.
public static TEnum ClearFlag<TEnum>(this TEnum value, TEnum flags) where TEnum : struct, Enum
Parameters
valueTEnumThe enumeration value to start from.
flagsTEnumThe flag bits to clear.
Returns
- TEnum
A new enumeration value equal to
valuewithflagsremoved.
Type Parameters
TEnumThe enumeration type.
Remarks
Equivalent to value & ~flags. Bits not present in flags are left unchanged, and
clearing a bit that was not set is a no-op.
GetDescription<TEnum>(TEnum)
Returns the description text for the specified enumeration value.
public static string GetDescription<TEnum>(this TEnum value) where TEnum : struct, Enum
Parameters
valueTEnumThe value to describe.
Returns
- string
The value's DescriptionAttribute text, or its DisplayAttribute name or short name, falling back to the value's name.
Type Parameters
TEnumThe enumeration type.
Remarks
Only values that map to a single declared enumeration field carry a description. A composite [Flags]
value with no declared field of its own (for example Read | Write) is not a declared field, so it falls
back to ToString().
GetDisplayName<TEnum>(TEnum)
Returns the display name for the specified enumeration value.
public static string GetDisplayName<TEnum>(this TEnum value) where TEnum : struct, Enum
Parameters
valueTEnumThe value to name.
Returns
- string
The value's DisplayAttribute name, or its DescriptionAttribute text, falling back to the value's name.
Type Parameters
TEnumThe enumeration type.
Remarks
Only values that map to a single declared enumeration field carry a display name. A composite [Flags]
value with no declared field of its own (for example Read | Write) is not a declared field, so it falls
back to ToString().
HasAllFlags<TEnum>(TEnum, TEnum)
Determines whether value has every bit set that is set in flags.
public static bool HasAllFlags<TEnum>(this TEnum value, TEnum flags) where TEnum : struct, Enum
Parameters
valueTEnumThe enumeration value to test.
flagsTEnumThe flag bits that must all be present.
Returns
Type Parameters
TEnumThe enumeration type.
Remarks
Equivalent to (value & flags) == flags evaluated on the underlying integer type. When
flags has no bits set the result is always true. This is the non-boxing
counterpart of HasFlag(Enum).
HasAnyFlag<TEnum>(TEnum, TEnum)
Determines whether value has at least one of the bits set in flags.
public static bool HasAnyFlag<TEnum>(this TEnum value, TEnum flags) where TEnum : struct, Enum
Parameters
valueTEnumThe enumeration value to test.
flagsTEnumThe flag bits to look for.
Returns
Type Parameters
TEnumThe enumeration type.
Remarks
Equivalent to (value & flags) != 0 evaluated on the underlying integer type. When
flags has no bits set the result is always false.
SetFlag<TEnum>(TEnum, TEnum)
Returns a copy of value with every bit in flags set.
public static TEnum SetFlag<TEnum>(this TEnum value, TEnum flags) where TEnum : struct, Enum
Parameters
valueTEnumThe enumeration value to start from.
flagsTEnumThe flag bits to set.
Returns
- TEnum
A new enumeration value equal to
valuewithflagsadded.
Type Parameters
TEnumThe enumeration type.
Remarks
Equivalent to the bitwise OR value | flags. The receiver is not modified; enumerations are value types
and the result is returned as a new value.
ToggleFlag<TEnum>(TEnum, TEnum)
Returns a copy of value with every bit in flags flipped.
public static TEnum ToggleFlag<TEnum>(this TEnum value, TEnum flags) where TEnum : struct, Enum
Parameters
valueTEnumThe enumeration value to start from.
flagsTEnumThe flag bits to toggle.
Returns
- TEnum
A new enumeration value equal to
valuewithflagstoggled.
Type Parameters
TEnumThe enumeration type.
Remarks
Equivalent to the bitwise XOR value ^ flags: bits present in flags are flipped, so
applying the operation twice with the same flags restores the original value.
TryGetDescription<TEnum>(TEnum, out string)
Attempts to return the explicit description text declared for the specified enumeration value.
public static bool TryGetDescription<TEnum>(this TEnum value, out string description) where TEnum : struct, Enum
Parameters
valueTEnumThe value to describe.
descriptionstringWhen this method returns, contains the explicit description if one is declared; otherwise, the value's name.
Returns
Type Parameters
TEnumThe enumeration type.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |