Flags Enum attribute

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

-------------Problems Reply------------

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

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

Category:c# Views:0 Time:2010-07-30
Tags: c# enums flags

Related post

  • What does the [Flags] Enum Attribute mean in C#? 2008-08-12

    From time to time I see an enum like the following: [Flags] public enum Options { None = 0, Option1 = 1, Option2 = 2, Option3 = 4, Option4 = 8 } I don't understand what exactly the [Flags]-attribute does. Anyone have a good explanation or example the

  • how to expand flagged enum 2009-02-16

    I have a key combination like this Keys key=Keys.Control | Keys.Shift | Keys.D ... I don't know how to expand key variable to separated Keys values. Maybe like this foreach(Keys k in key) { MessageBox.Show(k.ToString()); } this seems stupid to me. Ho

  • Efficient way to find the flags enum length? 2009-08-26

    Consider this: [Flags] enum Colors { Red=1, Green=2, Blue=4 } Colors myColor=Colors.Red|Colors.Blue; Currently, I'm doing it as follows: int length=myColors.ToString().Split(new char[]{','}).Length; But I hope there is a more efficient way of finding

  • WPF DataBinding to Flag Enum (in a PropertyGrid) 2009-09-24

    I need to have the ability to select multiple values as is the nature of a Flag enumeration from a WPF view (all be it, in a PropertyGrid). The properties in question are dynamic and no pre-defined DataTemplates can be used as the type of the propert

  • Random value from Flags enum 2010-06-08

    Say I have a function that accepts an enum decorated with the Flags attribute. If the value of the enum is a combination of more than one of the enum elements how can I extract one of those elements at random? I have the following but it seems there

  • Iterate over values in Flags Enum? 2010-11-13

    If I have a variable holding a flags enum, can I somehow iterate over the bit values in that specific variable? Or do I have to use Enum.GetValues to iterate over the entire enum and check which ones are set? --------------Solutions------------- stat

  • Enum.IsDefined with flagged enums 2011-02-09

    I'm currently reading the book C# 4.0 in a Nutshell, which by the way I think is an excellent book, even for advanced programmers to use as a good reference. I was looking back on the chapters about the basics, and I came across a trick to tell if a

  • Flag enums without power of two values 2011-08-20

    The MSDN documentation for the Flag attribute says that you should: Define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on. This means the individual flags in combined enumeration constants do not overlap. ...and of course I al

  • how to create flags enum from int values and disable controls 2013-05-29

    I have a small problem. This is a simplified version of my problem. I have a class that has a list of buttons with its names (New,Edit,Delete) as well a object of type "Type" class test { private List<Button> buttons; // private Type type; publ

  • C#, Flags Enum, Generic function to look for a flag 2009-06-12

    I'd like one general purpose function that could be used with any Flags style enum to see if a flag exists. This doesn't compile, but if anyone has a suggestion, I'd appreciate it. public static Boolean IsEnumFlagPresent<T>(T value,T lookingFor

  • Flags enum & bitwise operations vs. "string of bits" 2009-08-17

    A fellow developer suggested we store a selection of days of the week as 7-character string of 1’s and 0’s, i.e. “1000100” for Monday and Friday. I preferred (and strongly suggested) a solution with a Flags enum and bitwise operations, I think it's a

  • How to use flags enums in Linq to Entities queries? 2009-09-15

    I have a [Flags] enum like this: [Flags] public enum Status { None = 0, Active = 1, Inactive = 2, Unknown = 4 } A Status enum may contain two values such as: Status s = Status.Active | Status.Unknown; Now I need to create a linq query (LINQ to ADO.NE

  • Error while using ProtoBuf-Net with flags enum 2009-09-29

    While using ProtoBuf-Net and serializing an enum property, where the enum is set to [FlagsAttribute], I received the following error message when serializing an enum value composed of multiple flags. The error is: The value (MyEnum.MyValue) has no wi

  • Testing a [Flags] enum value for a single value 2009-11-02

    If I have an enum that's marked with [Flags], is there a way in .NET to test a value of this type to see if it only contains a single value? I can get the result I want using bit-counting, but I'd rather use built-in functions if possible. When loopi

  • ASP.NET control property with [Flags] enum 2009-12-01

    I have developed an ASP.NET control for which one of the properties is a [Flags] enum. However, I don't seem to be able to specify multiple flags for this property in the ASP.NET control markup. Is there a special syntax to do this or is it just not

  • How to Expose Enum attributes - WCF 2010-01-07

    I want to expose enum attributes to WCF client application, but I can only see enum values. Here is the enum: public enum TemplateType { [EnumDescription("Property Particulars")] [EnumValue("PropertyParticulars")] PropertyParticulars = 1, [EnumDescri

  • Fluent Nhibernate expression to select on flagged enum 2010-04-07

    I have a domain entity that has a flagged enum as a property. The flagged enum is the target audience for the these entities. The user then has a flagged enum value of the entities they should see. I am trying to figure out the correct expression to

  • C# - Help with [Flags] Enum & Extension Method 2010-10-06

    I have the following enum: [Flags] public enum PostAssociations { None = 0x0, User = 0x1, Comments = 0x2, CommentsUser = 0x3 } As a starting note, im not sure if those flags are correct. I'm doing this so that i have a fluent way of defining "Include

  • Flag Enum and mutually exclusive Enum with overlapping meaning 2010-10-12

    I have a model that must be in one of the following mutually exclusive states: New, In Progress, or Closed. The application allows a user to save a record and then retrieve them by providing a list of matching states. I inherited an SQL database wher

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.140 (s). 11 q(s)