Understanding Enums in C#

Enum are known as the named constant. If we have some constant which are related to each other then we can use Enum to group all constant. Enums are strongly typed constants which makes the code more readable and less prone to errors. It is useful when you have a set of values that are functionally significant and unchanged.

Let us take some example on Enum

Example 1

  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     Console.WriteLine(Enum_Name.Sun);  
  9.     Console.WriteLine(Enum_Name.Mon);  
  10.     Console.WriteLine(Enum_Name.Tue);  
  11.     Console.WriteLine(Enum_Name.Wed);  
  12.     Console.WriteLine(Enum_Name.Thu);  
  13.     Console.WriteLine(Enum_Name.Fri);  
  14.     Console.WriteLine(Enum_Name.Sat);  
  15.     Console.WriteLine((int) Enum_Name.Sun);  
  16.     Console.WriteLine((int) Enum_Name.Mon);  
  17.     Console.WriteLine((int) Enum_Name.Tue);  
  18.     Console.WriteLine((int) Enum_Name.Wed);  
  19.     Console.WriteLine((int) Enum_Name.Thu);  
  20.     Console.WriteLine((int) Enum_Name.Fri);  
  21.     Console.WriteLine((int) Enum_Name.Sat);  
  22.     Console.ReadLine();  
  23. }  
Output
 

Example 2

  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed = 100, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     Console.WriteLine((int) Enum_Name.Sun);  
  9.     Console.WriteLine((int) Enum_Name.Mon);  
  10.     Console.WriteLine((int) Enum_Name.Tue);  
  11.     Console.WriteLine((int) Enum_Name.Wed);  
  12.     Console.WriteLine((int) Enum_Name.Thu);  
  13.     Console.WriteLine((int) Enum_Name.Fri);  
  14.     Console.WriteLine((int) Enum_Name.Sat);  
  15.     Console.ReadLine();  
  16. }  
Output
 

Example 3

  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed = 10000000000, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     Console.WriteLine((int) Enum_Name.Sun);  
  9.     Console.WriteLine((int) Enum_Name.Mon);  
  10.     Console.WriteLine((int) Enum_Name.Tue);  
  11.     Console.WriteLine((int) Enum_Name.Wed);  
  12.     Console.WriteLine((int) Enum_Name.Thu);  
  13.     Console.WriteLine((int) Enum_Name.Fri);  
  14.     Console.WriteLine((int) Enum_Name.Sat);  
  15.     Console.ReadLine();  
  16. }  
Output
 

Above program will throw an error because by default data type of an enum is integer. In above example we exceed the range of integer so it throw an error . For removing this error we type cast the enum.

  1. enum Enum_Name: long  
  2. {  
  3.     Sun, Mon, Tue, Wed = 10000000000, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     Console.WriteLine((int) Enum_Name.Sun);  
  9.     Console.WriteLine((int) Enum_Name.Mon);  
  10.     Console.WriteLine((int) Enum_Name.Tue);  
  11.     Console.WriteLine((long) Enum_Name.Wed);  
  12.     Console.WriteLine((long) Enum_Name.Thu);  
  13.     Console.WriteLine((long) Enum_Name.Fri);  
  14.     Console.WriteLine((long) Enum_Name.Sat);  
  15.     Console.ReadLine();  
  16. }   

Output

Example 4

  1. enum Enum_Name: long  
  2. {  
  3.     Sun = 25, Mon, Tue = Sun, Wed = Sun, Thu = Sun, Fri = Sun, Sat = Sun  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     Console.WriteLine((int) Enum_Name.Sun);  
  9.     Console.WriteLine((int) Enum_Name.Mon);  
  10.     Console.WriteLine((int) Enum_Name.Tue);  
  11.     Console.WriteLine((long) Enum_Name.Wed);  
  12.     Console.WriteLine((long) Enum_Name.Thu);  
  13.     Console.WriteLine((long) Enum_Name.Fri);  
  14.     Console.WriteLine((long) Enum_Name.Sat);  
  15.     Console.ReadLine();  
  16. }  

Output

In enum we can define the members of the enum with the predefined members of enums.

Example 5

  1. enum Enum_Name: long  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat, Sun  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     Console.WriteLine((int) Enum_Name.Sun);  
  9.     Console.WriteLine((int) Enum_Name.Mon);  
  10.     Console.WriteLine((int) Enum_Name.Tue);  
  11.     Console.WriteLine((long) Enum_Name.Wed);  
  12.     Console.WriteLine((long) Enum_Name.Thu);  
  13.     Console.WriteLine((long) Enum_Name.Fri);  
  14.     Console.WriteLine((long) Enum_Name.Sat);  
  15.     Console.ReadLine();  
  16. }  

Output

In an enum we can’t define same element more then one times.

Example 6

  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     String[] Str = Enum_Name.GetNames(typeof(Enum_Name));  
  9.     foreach(string s in Str)  
  10.     System.Console.WriteLine(s);  
  11.     Console.ReadLine();  
  12. }  

Output

In above we show that how we can access the element of an Enum using foreach loop.

Example 7

  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     Console.WriteLine(Enum_Name.Sun + Enum_Name.Thu);  
  9.     Console.ReadLine();  
  10. }  

Output

C# doesnot allow us to add two members of an enum as shown in the above program. In order to do so we need to overload the plus operator.

We can add two element of an enum using type casting.

  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     Console.WriteLine((int) Enum_Name.Sun + (int) Enum_Name.Thu);  
  9.     Console.ReadLine();  
  10. }  

Output

4

Example 8

  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. enum Enum_Name2  
  7. {  
  8.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  9. };  
  10.   
  11. static void Main(string[] args)  
  12. {  
  13.     Console.WriteLine(Enum_Name.Sun == Enum_Name2.Sun);  
  14.     Console.ReadLine();  
  15. }  

Output

We can not compare the element of two different Enum.

Example 9

  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.     Console.WriteLine(Enum_Name.Sun > Enum_Name.Thu);  
  9.     Console.WriteLine(Enum_Name.Sat == Enum_Name.Sat);  
  10.     Console.WriteLine(Enum_Name.Sun < Enum_Name.Thu);  
  11.     Console.ReadLine();  
  12. }  

Output

 
We can compare the two element of same enum.
Up Next
Ebook Download
View all
Learn
View all