7

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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

5

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

1 Comment

Offtopic, but I just noticed you are the first one to cross 200 000 reputation. Congrats:)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.