C# Enum
An enum is a set of named integer constants. The enum type is declared using the enum keyword.
C# enums are value types. In other words, enums contain their own values and cannot be inherited or passed inheritance.
Declaring an enum Variable
The general syntax for declaring an enum is:
enum <enum_name>
{
enumeration list
};
Where,
enum_name specifies the type name of the enum.
enumeration list is a comma-separated list of identifiers.
Each symbol in the enumeration list represents an integer value, one greater than the symbol preceding it. By default, the value of the first enumeration symbol is 0. For example:
enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
Example
The following example demonstrates the usage of enum variables:
using System;
public class EnumTest
{
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
static void Main()
{
int x = (int)Day.Sun;
int y = (int)Day.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
}
}
When the above code is compiled and executed, it produces the following result:
Sun = 0
Fri = 5