A Flags enumeration in C# is a specialized type ofDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
enum decorated with the [System.Flags] attribute, designed to treat the enumeration as a bit field. This allows a single enumeration variable to store a combination of multiple values simultaneously using bitwise operations, rather than being restricted to a single, mutually exclusive constant.
To function correctly as a bit field, the underlying integer values of the enumeration members must be defined as powers of two (1, 2, 4, 8, 16, etc.). This ensures that each value occupies exactly one unique bit in the binary representation of the underlying integer type.
Declaration Syntax
When defining a Flags enum, developers typically use either explicit integer assignment or the bitwise left-shift operator (<<) to guarantee non-overlapping bit positions. It is standard practice to include a None member assigned to 0 to represent an empty bit field.
The [Flags] Attribute Mechanics
The [Flags] attribute does not alter the underlying bitwise arithmetic; the bitwise operators will function on any integer-backed enum. Instead, the attribute modifies the metadata of the type and alters the behavior of System.Enum.ToString():
Enum.ToString(): When called on a standard enum containing a combined bitwise value that lacks a defined named constant, it returns the raw integer string. When called on an enum decorated with[Flags], the runtime parses the bit field and returns a comma-separated string of the active member names.
Enum.Parse() and Enum.TryParse() natively support parsing comma-separated strings into combined bitwise values for all enums. This parsing capability is built into the System.Enum class and is not dependent on the presence of the [Flags] attribute.)
Bitwise Operations
Manipulating and evaluating a Flags enum requires standard bitwise operators.1. Combining Values (Bitwise OR |)
The | operator merges the bit patterns of two or more flags. If a bit is 1 in either operand, it becomes 1 in the result.
2. Checking Values (Bitwise AND & or HasFlag)
To determine if a specific bit is set, use the & operator to mask the enum, then compare the result to the target flag. Alternatively, the .NET Base Class Library provides the Enum.HasFlag() method. Starting in .NET Core 2.1, the .NET JIT compiler recognizes HasFlag as an intrinsic and optimizes it into the equivalent bitwise CPU instructions, avoiding the overhead of a standard method call.
Checking for None: Checking for an empty bit field (None or 0) requires a direct equality check. Using the bitwise AND approach (activeFlags & ConfigurationFlags.None) == ConfigurationFlags.None or activeFlags.HasFlag(ConfigurationFlags.None) will always evaluate to true regardless of the active flags, because bitwise operations against zero always yield zero.
3. Removing Values (Bitwise AND with Bitwise NOT & ~)
To unset a specific flag while leaving the rest of the bit field intact, combine the & operator with the bitwise complement operator (~). The ~ operator inverts the target flag’s bits, and the & operator applies that mask to clear only the target bit.
4. Toggling Values (Bitwise XOR ^)
The ^ operator flips the state of a specific bit. If the flag is currently set, it will be removed. If it is not set, it will be added.
Master C# with Deep Grasping Methodology!Learn More





