Mladen Prajdić Blog

Blog about stuff and things and stuff. Mostly about SQL server and .Net

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.

Legacy Comments


ML
2007-01-12
re: Why do we need a [Flags] attribute on enums?
Should a comma denote the logical AND? ;)

Mladen
2007-01-12
re: Why do we need a [Flags] attribute on enums?
AND, OR, XOR, NOT... it's all the same. :)))

David
2007-01-12
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. :)

Mladen
2007-01-12
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

David
2007-01-13
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.

Miha Markic
2007-01-31
re: Why do we need a [Flags] attribute on enums?
I use to decorate enum values with Description attribute and get description from there.

Georgia
2007-07-18
re: Why do we need a [Flags] attribute on enums?
Flags are to represent your country

peter
2009-11-20
re: Why do we need a [Flags] attribute on enums?
Very good post - very interesting. I can see myself using this a lot. I've just moved from VB to C#. Enums are under-used in my experience.

lex!
2010-04-06
re: Why do we need a [Flags] attribute on enums?
why do every state NEED a state flag

Valera
2010-04-25
re: Why do we need a [Flags] attribute on enums?
Look here :
http://weblogs.asp.net/wim/archive/2004/04/07/109095.aspx

ella
2010-04-29
re: Why do we need a [Flags] attribute on enums?
all i want to know why we need a FLAG