Enum in C#

Enumerations are special sets of named values that all map to a set of numbers (generally integers). An enumeration improves the code clarity and makes programs easier to maintain. An enumeration can be defined using the Enum keyword. An enum consists of a set of named values called the Enumerator List.

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

By default, in an enum the first enumerator has the integer value 0 and the value of the next enumerator is increased by 1. But we can also assign specific values of each enumerator.

Syntax

Enum <Enum_Name>
{
Enumeration list
}

Example

  1. enum week  
  2. {  
  3.     Sunday,  
  4.     Monday,  
  5.     Tuesday,  
  6.     Wednesday,  
  7.     Thursday,  
  8.     Friday,  
  9.     Saturday,  
  10. }  
Or:
  1. enum week  
  2. {  
  3.     Sunday=1,  
  4.     Monday,  
  5.     Tuesday,  
  6.     Wednesday,  
  7.     Thursday,  
  8.     Friday,  
  9.     Saturday,  
  10. }  
Program 1
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         int Sunday = 1;  
  6.         int Monday = 2;  
  7.         int Tuesday = 3;  
  8.         int Wednesday = 4;  
  9.         int Thursday = 5;  
  10.         int Friday = 6;  
  11.         int Saturday = 7;  
  12.         Console.WriteLine((int)week.Sunday);  
  13.         Console.WriteLine((int)week.Monday);  
  14.         Console.WriteLine((int)week.Tuesday);  
  15.         Console.WriteLine((int)week.Wednesday);  
  16.         Console.WriteLine((int)week.Thursday);  
  17.         Console.WriteLine((int)week.Friday);  
  18.         Console.WriteLine((int)week.Saturday);  
  19.         Console.ReadKey();  
  20.     }  
  21. }  
Output

see result

In this program we must put the “=” sign and value for every variable that identifies a weekday. An enum provides a convenient way for doing that. We will use an enum to write the preceding program.
  1. class Program  
  2. {  
  3.     enum week  
  4.     {  
  5.         Sunday = 1,  
  6.         Monday,  
  7.         Tuesday,  
  8.         Wednesday,  
  9.         Thursday,  
  10.         Friday,  
  11.         Saturday,  
  12.     }  
  13.     static void Main(string[] args)  
  14.     {  
  15.         Console.WriteLine((int)week.Sunday);  
  16.         Console.WriteLine((int)week.Monday);  
  17.         Console.WriteLine((int)week.Tuesday);  
  18.         Console.WriteLine((int)week.Wednesday);  
  19.         Console.WriteLine((int)week.Thursday);  
  20.         Console.WriteLine((int)week.Friday);  
  21.         Console.WriteLine((int)week.Saturday);  
  22.         Console.ReadKey();  
  23.     }  
  24. }  
Output

see result

An enum allows us to create a new data type. In the preceding program you see a data type week created using an enum. An enum can be considered to be a static class. So we access the element within the enum using just the name of the enum week, like week.Sunday.

The following are some important points related to enums. 
  1. An enum cannot be derived from another enum.
    1.     enum WorkingDay  
    2.     {  
    3.         Monday,  
    4.         Tuesday,  
    5.         Wednesday,  
    6.         Thursday,  
    7.         Friday  
    8.     }  
    9.     enum week:WorkingDay  
    10.     {  
    11.         Saturday,  
    12.         Sunday,  
    13.     }  
    14. class Program  
    15. {  
    16.     static void Main(string[] args)  
    17.     {  
    18.   
    19.     }  
    20. }  
    Output

    program error
  2. An enum cannot be derived by a class (enums are treated as sealed classes).
    1.     enum week  
    2.     {  
    3.         Sunday,  
    4.         Monday,  
    5.         Tuesday,  
    6.         Wednesday,  
    7.         Thursday,  
    8.         Friday,  
    9.         Saturday,  
    10.     
    11.     }  
    12. class Program:week  
    13. {  
    14.     static void Main(string[] args)  
    15.     {  
    16.   
    17.     }  
    18. }  
    Output

    error descripyion

  3. We can assign a specific constant value for the enumerator of an enum (not floating point value).
    1. enum Weight  
    2. {  
    3.     Pankaj = 68,  
    4.     Lekhraj = 66,  
    5.     Raju = 60,  
    6.     Rahul = 50  
    7. }  
    8. class Program  
    9. {  
    10.     static void Main(string[] args)  
    11.     {  
    12.         Console.WriteLine("Weight of {0} = {1}", Weight.Pankaj, (int)Weight.Pankaj);  
    13.         Console.WriteLine("Weight of {0} = {1}", Weight.Lekhraj, (int)Weight.Lekhraj);  
    14.         Console.WriteLine("Weight of {0} = {1}", Weight.Raju, (int)Weight.Raju);  
    15.         Console.WriteLine("Weight of {0} = {1}", Weight.Rahul, (int)Weight.Rahul);  
    16.         Console.ReadKey();  
    17.     }  
    18. }  
    Output

    run program

  4. We can assign the same value for the enumerator of an enum.
    1. enum Weight  
    2. {  
    3.     Pankaj = 66,  
    4.     Lekhraj = 66,  
    5.     Raju = 66,  
    6.     Rahul = 50,  
    7.     Dheeraj  
    8. }  
    9. class Program  
    10. {  
    11.     static void Main(string[] args)  
    12.     {  
    13.         Console.WriteLine("Weight of {0} = {1}", Weight.Pankaj, (int)Weight.Pankaj);  
    14.         Console.WriteLine("Weight of {0} = {1}", Weight.Lekhraj, (int)Weight.Lekhraj);  
    15.         Console.WriteLine("Weight of {0} = {1}", Weight.Raju, (int)Weight.Raju);  
    16.         Console.WriteLine("Weight of {0} = {1}", Weight.Rahul, (int)Weight.Rahul);  
    17.         Console.WriteLine("Weight of {0} = {1}", Weight.Dheeraj, (int)Weight.Dheeraj);  
    18.         Console.ReadKey();  
    19.     }  
    Output

    run

  5. The value of an enumerator can't change. It behaves like a constant.
    1. enum Weight  
    2. {  
    3.     Pankaj = 68,  
    4.     Lekhraj = 66,  
    5.     Raju = 60,  
    6.     Rahul = 50  
    7. }  
    8. class Program  
    9. {  
    10.     static void Main(string[] args)  
    11.     {  
    12.         Weight.Lekhraj = 60;  
    13.         Console.WriteLine("Weight of {0} = {1}", Weight.Pankaj, (int)Weight.Pankaj);  
    14.         Console.WriteLine("Weight of {0} = {1}", Weight.Lekhraj, (int)Weight.Lekhraj);  
    15.         Console.WriteLine("Weight of {0} = {1}", Weight.Raju, (int)Weight.Raju);  
    16.         Console.WriteLine("Weight of {0} = {1}", Weight.Rahul, (int)Weight.Rahul);  
    17.         Console.ReadKey();  
    18.     }  
    19. }  
    Output

    error

  6. By default the type of each enumerator in the enum is an int, but we can specify another integral numeric type using “:” as in the following:
    1. enum Number : long  
    2. {  
    3.     Million = 1000000,  
    4.     Billion = 1000000000  
    5. }  
    6. class Program  
    7. {  
    8.     static void Main(string[] args)  
    9.     {  
    10.         Console.WriteLine("Million = {0}", (long)Number.Million);  
    11.         Console.WriteLine("Billion = {0}", (long)Number.Billion);  
    12.         Console.ReadKey();  
    13.     }  
    14. }  
    output


Similar Articles