"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, fourOkay, 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:
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.

