Export (0) Print
Expand All


The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type except char. This declaration takes the following form::

[attributes] [modifiers] enum identifier [:base-type] {enumerator-list} [;]


attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see 17. Attributes.
modifiers (Optional)
The allowed modifiers are new and the four access modifiers.
The enum name.
base-type (Optional)
The underlying type that specifies the storage allocated for each enumerator. It can be one of the integral types except char. The default is int.
The enumerators' identifiers separated by commas, optionally including a value assignment.


The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example:

enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth. Enumerators can have initializers to override the default values. For example:

enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, the sequence of elements is forced to start from 1 instead of 0.

The default value of an enum E is the value produced by the expression (E)0.

The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type to an integral type. For example, the following statement assigns the enumerator Sun to a variable of the type int using a cast to convert from enum to int:

int x = (int) Days.Sun;

For more information on enumeration types, see 14. Enums.

Example 1

In this example, an enumeration, Days, is declared. Two enumerators are explicitly converted to int and assigned to int variables.

// keyword_enum.cs
// enum initialization:
using System;
public class EnumTest 
   enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

   public static void Main() 
      int x = (int) Days.Sun;
      int y = (int) Days.Fri;
      Console.WriteLine("Sun = {0}", x);
      Console.WriteLine("Fri = {0}", y);


Sun = 2
Fri = 7

Notice that if you remove the initializer from Sat=1, the result will be:

Sun = 1
Fri = 6

Example 2

In this example, the base-type option is used to declare an enum whose members are of the type long. Notice that even though the underlying type of the enumeration is long, the enumeration members must still be explicitly converted to type long using a cast.

// keyword_enum2.cs
// Using long enumerators
using System;
public class EnumTest 
   enum Range :long {Max = 2147483648L, Min = 255L};
   public static void Main() 
      long x = (long) Range.Max;
      long y = (long) Range.Min;
      Console.WriteLine("Max = {0}", x);
      Console.WriteLine("Min = {0}", y);


Max = 2147483648
Min = 255

See Also

C# Keywords | Default Values Table | Built-in Types Table | Types | Value Types

© 2014 Microsoft