The Secret of [Flags] in C#: How to Write Code Even Juniors Will Understand

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What Lies Behind the [Flags] Attribute in C#?

\n\n

If 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?"

\n\n

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.

\n\n

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\n

What is [Flags] in C# Really?

\n\n

The [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).

\n\n

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\n

You 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.

\n\n

How Bit Magic Works

\n\n

The values 1, 2, 4, 8 are not chosen randomly. In binary, they are:

\n
    \n
  • 1 = 0001
  • \n
  • 2 = 0010
  • \n
  • 4 = 0100
  • \n
  • 8 = 1000
  • \n
\n\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."

\n\n

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.

\n\n

Why Do You Need [Flags] if You Can Just Use Numbers?

\n\n

Yes, 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, Write instead of the number 3.
  • \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
\n\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\n

Best Practices for Working with [Flags] in 2025

\n\n

1. Always Use Powers of Two

\n\n

This is rule number one. Values 1, 2, 4, 8, 16, 32... — and nothing else. For convenience, you can use hexadecimal notation:

\n\n
[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\n

Note: always include None = 0. Without it, your enum will default to 0, which could mean "nothing selected."

\n\n

2. Use Enum.HasFlag() — But With Ca

Blogs

Book Recommendations