"Flags" Attribute -- Useless?

Hi everyone,

I've read all about theFlagsAttribute and it seems hardly useful.

From the examples I've seen and the ones I've written myself, I can see only one simple case where using theFlagsAttribute is different from not using it:When you print an instance of a the "FlagsAttribute"-decoratedenum, the resulting string contains the enum constants whose bits are set, rather than showing the underlying numeric value.

Have a look at this snippet:

[Flags]

enum Fred : short {

zero, one, two, four = 4, eight = 8

}

class C_01 {

private static void Main() {

Fred f = (Fred)7;

Console.WriteLine("f is " + f);

} // Main()

} // class C_01

When I run this snippet, I see this output:

f is one, two, four

Okay, that's very nice. The string representation of theenum shows me exactly which bits are set. Wonderful. If I eliminate the[Flags] attribute the output changes to the bare numeric value:

f is 7

So, my question is, what else does the[Flags] attribute buy me? Is there something I can do with a[Flags]-decoratedenum that I couldn't do otherwise? Is the only purpose of the[Flags] attribute to provide a textual description of the bits that are set? If so, it seems like a marginally useful mechanism, at best.

[1670 byte] By [AFoxmore] at [2007-12-22]
# 1

Not sure what you are after but if you check MSDN it is quite straight forward what FlagsAttribute does.
http://msdn2.microsoft.com/en-us/library/system.flagsattribute.aspx

FlagsAttribute allows you to combine values (bit mask) instead of identifying a single value.

The MSDN entry clearly demonstrates the difference between enums with and without this attribute.

AndreasJohansson at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2

Hi,

The FlagsAttribute is defined in the System namespace. Adorning the enumerator with this attribute will permit us to treat the variables as bit fields. As a bit field, we are permitted to assign bitwise combinations of the values in the enumeration. Without the flag, we technically are only supposed to be able to assign one value from the enumeration at a time.

Also if you publish it to be used in webservices it will create the contents for you (it will assign 0x01, 0x02, 0x04, 0x08 and so on) without you specifying them.

Not other useful function I found either.

Cheers

SalvaPatuel at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# Language...
# 3

Although C# happily allows users to perform bit operations on enums without the FlagsAttribute, Visual Basic does not. So if you are exposing types to other languages, then marking enums with the FlagsAttribute is a good idea; it also makes it clear that the members of the enum are designed to be used together.

Regards

David

DavidM.Kean-MSFT at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# Language...
# 4

Did you even read my post?

The MSDN entry does not demonstrate anything other than what I already said -- there is a difference in the textual representation between "FlagsAttribute" enums and non-"FlagsAttribute" enums. That is *all* the MSDN entry illustrates, nothing more.

You said , "FlagsAttribute allows you to combine values (bit mask) instead of identifying a single value." Yes, this is true, but you can combine values with a enum that is *not* decorated just as easily. Show me something I can do with a FlagsAttribute that I cannot do with a non-FlagsAttribute.

AFoxmore at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# Language...
# 5
Interesting post. I never thought of using Flags but in my current project I can see great use for it. I also guess that Flags allow for a greater interoperability when used in webservices, crossing language and os bounderies.
theking2 at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# Language...
# 6

Create a file a.txt which is archive and read only, put in the appropriate directory. Run the following code. Comment out the flags attribute and run again. Status words are often stored as sets of flags and the attribute is useful for checking which are set.

using System;

using System.IO;

class FlagsAttributeDemo

{

[Flags]

enum CommonAttributes : short

{

Archive = 1,

Hidden = 2,

ReadOnly = 32,

Normal = 128

}

static void Main()

{

string filename = "a.txt";

CommonAttributes attr;

attr = (CommonAttributes)File.GetAttributes(filename);

Console.WriteLine(attr);

}

}

CliftonJones at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# Language...
# 7

Create a file a.txt which is archive and read only, put in the appropriate directory. Run the following code. Comment out the flags attribute and run again. Status words are often stored as sets of flags and the attribute is useful for checking which are set.

using System;

using System.IO;

class FlagsAttributeDemo

{

[Flags]

enum CommonAttributes : short

{

Archive = 1,

Hidden = 2,

ReadOnly = 32,

Normal = 128

}

static void Main()

{

string filename = "a.txt";

CommonAttributes attr;

attr = (CommonAttributes)File.GetAttributes(filename);

Console.WriteLine(attr);

}

}

CliftonJones at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# Language...