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?
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).