3

According to my code a=1, b=2, c=3 etc. I thought the flag would make a=1, b=2, c=4, etc

[Flags]
public enum someEnum { none, a, b, c, d, e, f, }

How do i get what i intended(c=4, e=8)? and what does the [Flags] mean above?

Aamir
  • 14,882
  • 6
  • 45
  • 69

4 Answers4

17

You can specify values for the enums, this is needed for flags cases:

[Flags]
enum MyFlags {
  Alpha=1,
  Beta=2,
  Gamma=4,
  Delta=8
}

what does the [Flags] mean above?

It means the runtime will support bitwise operations on the values. It makes no difference to the values the compiler will generate. E.g. if you do this

var x = MyFlags.Alpha | MyFlags.Beta;

with the Flags attribute the result of x.ToString() is "Alpha, Beta". Without the attribute it would be 3. Also changes parsing behaviour.

EDIT: Updated with better names, and the compiler doesn't complain using bitwise ops on a non-flags attribute, at least not C#3 or 4 (news to me).

Richard
  • 106,783
  • 21
  • 203
  • 265
  • In addendum: this means that you can have: var flags = MyFlags.a | myFlags.b i.e. "flags" have both values (a and b) – veggerby Aug 26 '09 at 08:17
  • When [Flags] is used to identify a combinable enum, it is a convention to have a pluralised name for the enum - myflags you can set the underlying type of the enum (default int) to another integral value. e.g: enum myenum : byte – AndyM Aug 26 '09 at 08:17
  • BTW, the compiler allows bitwise operations on enums even if the Flags attribute is not specified. It is more an informational attribute than a real constraint (although some classes, like XmlSerializer, require that attribute to work with bitwise combinations) – Thomas Levesque Aug 26 '09 at 08:24
  • 3
    btw, for this usage it is recommended to also include a `None = 0,` entry (or similar) – Marc Gravell Aug 26 '09 at 08:26
  • 1
    Instead of setting 1,2,4,8 you can use 1<<0 , 1<<1 , 1<<2 , 1<<3. This is specially useful when you have a lot of enums and you have to define very large numbers. – pauloya Jan 28 '10 at 13:21
1

Flags Attribute indicates that an enumeration can be treated as a bit field; that is, a set of flags.

Svetlozar Angelov
  • 21,214
  • 6
  • 62
  • 67
1

The Flags attribute really only affects the behaviour/output of the ToString(), Parse() and IsDefined() methods on your enumeration.

You can perform bitwise operations without using the Flags attribute, as long as you use powers of two values.

Read this existing question (and answers) for more detailed coverage.

Community
  • 1
  • 1
Ash
  • 60,973
  • 31
  • 151
  • 169
0

[Flags] does not alter the way in which the compiler assigns values to each identifier, it is always your responsibility to make sure that each value is appropriate, whether the enum contains flags or not.

It also does not alter the bitwise operations that the compiler will allow on the enum.

What it does do is alter the behaviour of the Enum.Parse and Enum.ToString methods, to support combinations of values as well as single values, e.g. "a, b, c".

Christian Hayter
  • 30,581
  • 6
  • 72
  • 99