Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Enum

C# Enum

An enum is a set of named integer constants. An enum type is declared using the enum keyword.

C# enums are value types. In other words, an enum contains its own value and cannot inherit or be inherited.

Declaration enum Variables

The general syntax for declaring an enum:


enum 
{ 
    enumeration list 
};

Where

Each symbol in the enum list represents an integer value, an integer value larger than the symbol before it. By default, the value of the first enum symbol is 0. For example:


enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

Example

The following example demonstrates the use of enum variables:

Example

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 code above is compiled and executed, it produces the following result:


Sun = 0
Fri = 5