CSharp examples for Custom Type:Enum
You can convert an enum instance to and from its underlying integral value with an explicit cast:
public enum Direction : byte { Left=1, Right, Top=10, Bottom }
int i = (int) Direction.Left;
Direction side = (Direction) i;
bool leftOrRight = (int) side <= 2;
You can also explicitly cast one enum type to another.
public enum HorizontalAlignment
{
Left = Direction.Left,
Right = Direction.Right,
Center
}
A translation between the enum types uses the underlying integral values:
HorizontalAlignment h = (HorizontalAlignment) Direction.Right;
// same as:
HorizontalAlignment h = (HorizontalAlignment) (int) Direction.Right;
The numeric literal 0 is treated specially by the compiler in an enum expression and does not require an explicit cast.
The first member of an enum is often used as the "default" value.
public enum Direction : byte { Left=1, Right, Top=10, Bottom } Direction b = 0; // No cast required if (b == 0) ...