CSharp/C# Tutorial - C# Enums






An enum is a special value type that groups of named numeric constants.

Example

For example:

public enum Direction { Left, Right, Top, Bottom } 

We can use this enum type as follows:

Direction topSide = Direction.Top; 
bool isTop = (topSide == Direction.Top); // true 

underlying integral value

Each enum member has an underlying integral value.

By default, Underlying values are of type int.

The constants 0, 1, 2... are automatically assigned, in the declaration order of the enum members.

We may specify an alternative integral type, as follows:

public enum Direction : byte { 
   Left, Right, Top, Bottom 
}

We may also specify an explicit underlying value for each enum member:

public enum Direction : byte { 
   Left=1, Right=2, Top=10, Bottom=11 
} 

The compiler also lets us explicitly assign some of the enum members.

The unassigned enum members keep incrementing from the last explicit value.

The preceding example is equivalent to the following:

public enum Direction : byte { 
   Left=1, Right, Top=10, Bottom 
} 




Enum Conversions

We can convert an enum instance to and from its underlying integral value with an explicit cast:

int i = (int) Direction.Left; 
Direction side = (Direction) i; 
bool leftOrRight = (int) side <= 2; 

The numeric literal 0 is treated specially by the compiler in an enum expression and does not require an explicit cast:

Direction b = 0; // No cast required 
if (b == 0) ... 

The first member of an enum is often used as the "default" value.

For combined enum types, 0 means "no flags."





Flags Enums

We can combine enum members.

To prevent ambiguities, members of a combinable enum require explicitly assigned values, typically in powers of two.

For example:

[Flags] 
public enum Directions { 
   None=0, Left=1, Right=2, Top=4, Bottom=8 
} 

To work with combined enum values, we use bitwise operators, such as | and &.

These operate on the underlying integral values:

Directions leftRight = Directions.Left | Directions.Right; 

if ((leftRight & Directions.Left) != 0) {
   Console.WriteLine ("Includes Left"); // Includes Left 
}
string formatted = leftRight.ToString(); // "Left, Right" 

Directions s = Directions.Left; 
s |= Directions.Right; 
Console.WriteLine (s == leftRight); // True 
s ^= Directions.Right; // Toggles Directions.Right 
Console.WriteLine (s); // Left 

By convention, the Flags attribute should always be applied to an enum type when its members are combinable.

For convenience, you can include combination members within an enum declaration itself:

[Flags] 
public enum Directions {
    None=0,  Left=1, Right=2, Top=4, Bottom=8, 
    LeftRight = Left | Right, 
    TopBottom = Top | Bottom, 
    All = LeftRight | TopBottom 
} 

Enum Operators

The operators that work with enums are:

= == != < > <= >= + -^ & | ? 
+= -= ++ -- sizeof 

The bitwise, arithmetic, and comparison operators return the result of processing the underlying integral values.

Addition is permitted between an enum and an integral type, but not between two enums.