tags:

views:

87

answers:

2

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

+7  A: 

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

cgreeno
"CSV value"..WTF :)
codymanix
+2  A: 

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

Jon Skeet
@Jon Skeet - Offtopic, but I just noticed you are the first one to cross 200 000 reputation. Congrats:)
Petar Minchev