What does the [Flags] attribute mean for enums in C#?
Hi everyone! I just started learning C# and came across the `[Flags]` attribute before an enum declaration. I saw this code in examples, but I can't understand why it's needed and how to use it correctly.
I understand regular enums — they just define named constants:
```csharp
enum DayOfWeek
{
Monday = 1,
Tuesday = 2,
Wednesday = 3
}
```
But when I see this:
```csharp
[Flags]
enum Permissions
{
None = 0,
Read = 1,
Write = 2,
Execute = 4
}
```
I don't understand why `[Flags]` is here and why the values are set as powers of two (1, 2, 4). What's the difference from a regular enum? I tried using such an enum without the attribute — the code seems to work, but I feel like I'm doing something wrong.
Can you explain with simple examples?
No answers yet.
Log in to leave an answer