What Lies Behind the [Flags] Attribute in C#?
\n\nIf you've ever written in C# and seen a strange [Flags] attribute before an enum in someone else's code, you've probably wondered: "What if I don't put it — will the code stop working?"
Spoiler: it won't stop working. But your code will become like a bicycle with square wheels. Today we'll break down what the [Flags] attribute means in C#, how to use it correctly in 2025, and why without it your enums are just numbers, not meaningful combinations.
The topic comes from a real Stack Overflow question that gathered thousands of views. And it's no coincidence: proper use of bit flags is one of those skills that separates a developer who understands the language's internals from one who just "copy-pastes from MSDN."
\n\nWhat is [Flags] in C# Really?
\n\nThe [Flags] attribute is not a magic switch that changes the compiler's behavior. It is a marker for developers and tools (for example, for the Visual Studio debugger or console output).
When you write:
\n\n[Flags]\npublic enum FilePermissions\n{\n None = 0,\n Read = 1,\n Write = 2,\n Execute = 4,\n Delete = 8\n}\n\nYou are saying: "Hey, this enumeration is not about choosing one option, but about combining several." Without the [Flags] attribute, C# doesn't know you intend to do bitwise operations. But technically — you can.
How Bit Magic Works
\n\nThe values 1, 2, 4, 8 are not chosen randomly. In binary, they are:
- \n
1= 0001 \n 2= 0010 \n 4= 0100 \n 8= 1000 \n
Each flag occupies its own bit. Therefore, you can combine Read and Write as 1 | 2 = 3 (0011). And the system understands: "Oh, both flags are enabled."
If you had chosen values 1, 2, 3, 4 — the combination 1 | 2 = 3 would be indistinguishable from the value Execute = 3. That's why bit flags require powers of two.
Why Do You Need [Flags] if You Can Just Use Numbers?
\n\nYes, technically you can use bitwise operations without the attribute. But:
\n\n- \n
- Code readability:
permissions.HasFlag(FilePermissions.Write)is clearer than(permissions & 2) != 0. \n - Debugging: Visual Studio will show
Read, Writeinstead of the number3. \n - ToString(): calling
myFlags.ToString()will output "Read, Write" instead of "3". \n - Self-documentation: any developer seeing
[Flags]will immediately understand the intent. \n
Without the [Flags] attribute in C#, you lose all these benefits. The code works, but it looks like "I wrote this at 2 AM before the deadline."
\n\nBest Practices for Working with [Flags] in 2025
\n\n1. Always Use Powers of Two
\n\nThis is rule number one. Values 1, 2, 4, 8, 16, 32... — and nothing else. For convenience, you can use hexadecimal notation:
[Flags]\npublic enum DayOfWeek\n{\n None = 0,\n Monday = 0b_0000_0001, // 1\n Tuesday = 0b_0000_0010, // 2\n Wednesday = 0b_0000_0100, // 4\n Thursday = 0b_0000_1000, // 8\n Friday = 0b_0001_0000, // 16\n Weekend = Monday | Tuesday | Wednesday | Thursday | Friday\n}\n\nNote: always include None = 0. Without it, your enum will default to 0, which could mean "nothing selected."