Table of Contents

EnumExtensions Class

Definition

Namespace
Bodu.Extensions
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
EnumExtensions.BitConversion.cs

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

value TEnum

The enumeration value to start from.

flags TEnum

The flag bits to clear.

Returns

TEnum

A new enumeration value equal to value with flags removed.

Type Parameters

TEnum

The 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

value TEnum

The 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

TEnum

The 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

value TEnum

The value to name.

Returns

string

The value's DisplayAttribute name, or its DescriptionAttribute text, falling back to the value's name.

Type Parameters

TEnum

The 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

value TEnum

The enumeration value to test.

flags TEnum

The flag bits that must all be present.

Returns

bool

true if every bit set in flags is also set in value; otherwise, false.

Type Parameters

TEnum

The 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

value TEnum

The enumeration value to test.

flags TEnum

The flag bits to look for.

Returns

bool

true if value and flags share at least one set bit; otherwise, false.

Type Parameters

TEnum

The 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

value TEnum

The enumeration value to start from.

flags TEnum

The flag bits to set.

Returns

TEnum

A new enumeration value equal to value with flags added.

Type Parameters

TEnum

The 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

value TEnum

The enumeration value to start from.

flags TEnum

The flag bits to toggle.

Returns

TEnum

A new enumeration value equal to value with flags toggled.

Type Parameters

TEnum

The 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

value TEnum

The value to describe.

description string

When this method returns, contains the explicit description if one is declared; otherwise, the value's name.

Returns

bool

true if an explicit description or display attribute was declared; otherwise, false.

Type Parameters

TEnum

The enumeration type.

Applies to

ProductVersions
.NET8, 10