Udostępnij za pośrednictwem


enum (C# Reference)

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.

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. However, we strongly recommend that an enum contain a constant with a value of 0. For more information, see Enumeration Types (C# Programming Guide).

Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int. To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type:

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

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

A variable of type Days can be assigned any value in the range of the underlying type; the values are not limited to the named constants.

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

Note

An enumerator may not contain white space in its name.

The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is necessary 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 by using a cast to convert from enum to int:

int x = (int)Days.Sun;

When you apply System.FlagsAttribute to an enumeration that contains some elements combined with a bitwise OR operation, you will notice that the attribute affects the behavior of the enum when it is used with some tools. You can notice these changes when you use tools such as the Console class methods, the Expression Evaluator, and so forth. (See example 3).

Robust Programming

Just as with any constant, all references to the individual values of an enum are converted to numeric literals at compile time. This can create potential versioning issues as described in Constants (C# Programming Guide).

Assigning additional values to new versions of enums, or changing the values of the enum members in a new version, can cause problems for dependant source code. Enum values are used often in switch statements. If additional elements have been added to the enum type, the test for default values can return true unexpectedly.

If other developers will be using your code, you should provide guidelines about how their code should react if new elements are added to any enum types.

Example

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

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

    static void Main()
    {
        int x = (int)Days.Sun;
        int y = (int)Days.Fri;
        Console.WriteLine("Sun = {0}", x);
        Console.WriteLine("Fri = {0}", y);
    }
}
/* Output:
   Sun = 0
   Fri = 5
*/

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 by using a cast.

public class EnumTest2
{
    enum Range : long { Max = 2147483648L, Min = 255L };
    static void Main()
    {
        long x = (long)Range.Max;
        long y = (long)Range.Min;
        Console.WriteLine("Max = {0}", x);
        Console.WriteLine("Min = {0}", y);
    }
}
/* Output:
   Max = 2147483648
   Min = 255
*/

The following code example illustrates the use and effect of the System.FlagsAttribute attribute on an enum declaration.

[Flags]
public enum CarOptions
{
    SunRoof = 0x01,
    Spoiler = 0x02,
    FogLights = 0x04,
    TintedWindows = 0x08,
}

class FlagTest
{
    static void Main()
    {
        CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;
        Console.WriteLine(options);
        Console.WriteLine((int)options);
    }
}
/* Output:
   SunRoof, FogLights
   5
*/

Comments

Notice that if you remove FlagsAttribute, the example will output the following:

5

5

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.10 Enums

  • 6.2.2 Explicit Enumeration Conversions

  • 14 Enums

See Also

Tasks

Attributes Sample

Concepts

Enumeration Types (C# Programming Guide)

Enumeration Design

Reference

C# Keywords

Integral Types Table (C# Reference)

Built-In Types Table (C# Reference)

Implicit Numeric Conversions Table (C# Reference)

Explicit Numeric Conversions Table (C# Reference)

Other Resources

C# Reference