Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
title description ms.date f1_keywords helpviewer_keywords ms.assetid
Enumeration types - C# reference
Learn about C# enumeration types that represent a choice or a combination of choices
enum keyword [C#]
enum type [C#]
enumeration type [C#]
bit flags [C#]

Enumeration types (C# reference)

An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. To define an enumeration type, use the enum keyword and specify the names of enum members:

enum Season

By default, the associated constant values of enum members are of type int; they start with zero and increase by one following the definition text order. You can explicitly specify any other integral numeric type as an underlying type of an enumeration type. You can also explicitly specify the associated constant values, as the following example shows:

enum ErrorCode : ushort
    None = 0,
    Unknown = 1,
    ConnectionLost = 100,
    OutlierReading = 200

You cannot define a method inside the definition of an enumeration type. To add functionality to an enumeration type, create an extension method.

The default value of an enumeration type E is the value produced by expression (E)0, even if zero doesn't have the corresponding enum member.

You use an enumeration type to represent a choice from a set of mutually exclusive values or a combination of choices. To represent a combination of choices, define an enumeration type as bit flags.

Enumeration types as bit flags

If you want an enumeration type to represent a combination of choices, define enum members for those choices such that an individual choice is a bit field. That is, the associated values of those enum members should be the powers of two. Then, you can use the bitwise logical operators | or & to combine choices or intersect combinations of choices, respectively. To indicate that an enumeration type declares bit fields, apply the Flags attribute to it. As the following example shows, you can also include some typical combinations in the definition of an enumeration type.

[!code-csharpenum flags]

For more information and examples, see the xref:System.FlagsAttribute?displayProperty=nameWithType API reference page and the Non-exclusive members and the Flags attribute section of the xref:System.Enum?displayProperty=nameWithType API reference page.

The System.Enum type and enum constraint

The xref:System.Enum?displayProperty=nameWithType type is the abstract base class of all enumeration types. It provides a number of methods to get information about an enumeration type and its values. For more information and examples, see the xref:System.Enum?displayProperty=nameWithType API reference page.

You can use System.Enum in a base class constraint (that is known as the enum constraint) to specify that a type parameter is an enumeration type. Any enumeration type also satisfies the struct constraint, which is used to specify that a type parameter is a non-nullable value type.


For any enumeration type, there exist explicit conversions between the enumeration type and its underlying integral type. If you cast an enum value to its underlying type, the result is the associated integral value of an enum member.

[!code-csharpenum conversions]

Use the xref:System.Enum.IsDefined%2A?displayProperty=nameWithType method to determine whether an enumeration type contains an enum member with the certain associated value.

For any enumeration type, there exist boxing and unboxing conversions to and from the xref:System.Enum?displayProperty=nameWithType type, respectively.

C# language specification

For more information, see the following sections of the C# language specification:

See also