An enum is a custom data type of name-value pairs. An enumerator is a class or structure that implements a .NET Core interface named IEnumerable.
When building a system, it is often convenient to create a set of symbolic names that map to known numerical values.
Note: By convention, enum types are usually suffixed with Enum.
enum EmpTypeEnum
{
Manager, // 0
Grunt, // 1
Contractor, // 2
VicePresident // 3
}
By default the first element is set to the value zero (0), followed by an (n+1) progression.
enum EmpTypeEnum
{
Manager = 102;
Grunt, // 103
Contractor, // 104
VicePresident // 105
}
Enumerations do not necessarily need to follow a sequential ordering and do not to have unique values.
enum EmpTypeEnum
{
Manager = 10,
Grunt = 1,
Contractor = 100,
VicePresident = 9
}
Controlling the Underlying Storage for Enum
By default, the storage type used to hold values of an enumeration is a System.Int32(C# int). However, you are free to change this to your liking. C# enumerations can be defined in a similar manner for any of the core system types. (byte,short,int,or long).
// This time, EmpTypeEnum maps to an underyling byte.
enum EmpTypeEnum : byte
{
Manager = 10,
Grunt = 1,
Contractor = 100,
VicePresident = 9
}
Declaring enum Variables
// Make an EmpTypeEnum variable
EmpTypeEnum emp = EmpTypeEnum.Contractor;
Notice that when you are assigning a value to an enum variable, you must scope the enum name (EmpTypeEnum) to the value (Contractor).
Using the System.Enum Type
The interesting thing about .NET Core enumerations is that they gain functionality from the System.Enum class type.
Methods
| Type GetUnderlyingType(Type enumType) | Returns the underlying type of the specified enumeration. |
| string[] GetNames(Type enumType) | Retrieves an array of the names of the constants in a specified enumeration. |
| GetValues() | This method returns an instance of System.Array |
Dynamically Discover in an Enum's Name-Value Pairs
All C# enumerations support a method named ToString(), which returns the string name of the current enumeration's value.
EmpTypeEnum emp = EmpTypeEnum.Contractor;
Console.WriteLine(emp.ToString());
If you are interested in discovering the value of a given enumeration variable, rather than its name, you can simply cost the enum variable against the underlying storage type.
EmpTypeEnum emp_3 = EmpTypeEnum.VicePresident;
Console.WriteLine(emp_3.ToString() + " " + (byte)emp_3);
System.Enum also defines GetValues() method, this method returns an instance of System.Array class.
Using Enums, Flags, and Bitwise Operations
Bitwise operations provide a fast mechanism for operating on binary numbers at the bit level.
| Operator | Operation | Example |
| & (AND) | Copies a bit if it exists in both operands. | 0100 & 0100 = 0100 (4) |
| | (OR) | Copies a bit if it exists in both operands. | 0110 | 0100 = 0110 (6) |
| ^ (XOR) | Copies a bit if it exists in one but not both operands. | 0100 ^ 0100 = 0010 (2) |
- One's compliment
| Flips the bits | -0110 = -7 (due to overflow) | |
| << (left shift) | Shifts the bits left | 0110 << 1 = 1100 (12) |
| >> (right shirt) | Shifts the bits right | 0110 >> 1 = 0011 (3) |
namespace FunWithBitwiseOperation {
[Flags]
public enum ContactPreferenceEnum {
None=1,
Email=2,
Phone=4,
Text= 8
}
}
Notice the flags attribute. This allows multiple values from an enum to be combined into a single variable.
ContactPreferenceEnum emailAndPhone = ContactPreferenceEnum.Email | ContactPreferenceEnum.Text;
Console.WriteLine("None? {0}",(emailAndPhone | ContactPreferenceEnum.None) == emailAndPhone);
Console.WriteLine("Email? {0}",(emailAndPhone | ContactPreferenceEnum.Email) == emailAndPhone);
Console.WriteLine("Phone? {0}",(emailAndPhone | ContactPreferenceEnum.Phone) == emailAndPhone);
Console.WriteLine("Text? {0}",(emailAndPhone | ContactPreferenceEnum.Text) == emailAndPhone);