I want some Moore

Blog about stuff and things and stuff...
mostly about SQL server and .Net
posts - 156, comments - 1394, trackbacks - 33

My Links

SQLTeam.com Links

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer. I'm also a MCP and MCTS for SQL Server. 
Welcome to my blog.

Search this Blog
 

My Blog Feed via Email


Get your Google PageRank
Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

Why do we need a [Flags] attribute on enums?

Enums are a great tool. They give meaning to meaningless numbers.

I love using them as flags since it couldn't be simpler. So i always used the [Flags] attribute on my enums

But why do we need that attribute? You can bitwise non [Flags]'d enums just the same.

The difference lies in the Enum.ToString() method. If your enum has the [Flags] attribute set then the ToString() will return a

CSV separated list of bitwised enum values. If there's no [Flags] attribute ToString() will return a number for every bitwised value.

 

As always it's best ilustrated with an example:

[Flags]
enum StateWithFlags
{
    None = 0,
    Read = 1,
    Write = 2,
    Delete = 4
}

enum StateWithNoFlags
{
    None = 0,
    Read = 1,
    Write = 2,
    Delete = 4            
}

for (int enumValue = 0; enumValue <= 4; enumValue++)
    Console.WriteLine("StateWithFlags: " + enumValue.ToString() + " " + ((StateWithFlags)enumValue).ToString());
/*
this returns:
0 None
1 Read
2 Write
3 Read, Write
4 Delete
*/


for (int enumValue = 0; enumValue <= 4; enumValue++)
    Console.WriteLine("StateWithNoFlags: " + enumValue.ToString() + " " + ((StateWithNoFlags)enumValue).ToString());
/*
this returns:
0 None
1 Read
2 Write
3 3
4 Delete
*/

 

So we see that the flags attribute is quite handy when trying to simply display the combination of our choices.

Print | posted on Friday, January 12, 2007 5:40 PM

Feedback

# re: Why do we need a [Flags] attribute on enums?

Should a comma denote the logical AND? ;)
1/12/2007 8:27 PM | ML

# re: Why do we need a [Flags] attribute on enums?

AND, OR, XOR, NOT... it's all the same. :)))
1/12/2007 8:29 PM | Mladen

# re: Why do we need a [Flags] attribute on enums?

Wow! I did not know that. Quite cool. Now if out could just override enums .ToString(), that would make me happy. :)
1/12/2007 9:42 PM | David

# re: Why do we need a [Flags] attribute on enums?

no can do... believe me i tried :)

the closest i've come to it is this:
http://blogs.crankygoblin.com/blogs/geoff.appleby/archive/2004/11/18/32533.aspx
1/12/2007 9:57 PM | Mladen

# re: Why do we need a [Flags] attribute on enums?

I have tried it too, I know it's impossible. :) The workaarround is too messy imho. Nice, but I wouldn't use it.
1/13/2007 10:14 AM | David

# re: Why do we need a [Flags] attribute on enums?

I use to decorate enum values with Description attribute and get description from there.
1/31/2007 10:40 PM | Miha Markic

# re: Why do we need a [Flags] attribute on enums?

Flags are to represent your country
7/18/2007 2:41 AM | Georgia

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 1 and type the answer here:

Powered by: