Flags Enum attribute
Asked Answered
L

2

7

What is the point of the [Flags] attribute you can bit test without it?

Lamentable answered 30/7, 2010 at 11:41 Comment(0)
T
11

The Flags attribute allows you to see a CSV(comma separated value) of your enumerated type when calling ToString()

For Example:

[Flags]
public Enum Permissions
{
  None =0,
  Read = 1,
  Write =2,
  Delete= 4
}

Permissions p = Permissions.Read | Permissions.Write;
p.ToString() //Prints out "Read, Write"

However you can still get the same thing if you remove the flags attribute and just do:

p.ToString("F") //Prints out "Read, Write"

And as John pointed out it also allows you convert a CSV back to Enum using Enum.Parse

Titania answered 30/7, 2010 at 11:43 Comment(0)
P
5

It changes the behaviour of converting between strings and the enum values (Enum.Parse and ToString).

Paramnesia answered 30/7, 2010 at 11:43 Comment(1)
Offtopic, but I just noticed you are the first one to cross 200 000 reputation. Congrats:)Fructuous

© 2022 - 2024 — McMap. All rights reserved.