Enums In C#

In this article, I am going to share with you about Enums in C#.
 
Why should we use Enum?
 
Suppose we are creating a program in which we have to use multiple-named constants of similar type and on the basis of a particular constant, we have to perform some operation. So, what we normally do is to initialize each named constant with a value. This may require a lot of effort if the number of constants is large. Also, the look and feel of the code is not up to the mark. Have a look at the below program to understand more. The program contains the months of the year. We have to put a lot of efforts to assign each month to a value.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         int jan = 0;  
  8.         int feb = 1;  
  9.         int march = 2;  
  10.         int april = 3;  
  11.         int may = 4;  
  12.         int june = 5;  
  13.         int july = 6;  
  14.         int aug = 7;  
  15.         int sept = 8;  
  16.         int oct = 9;  
  17.         int nov = 10;  
  18.         int dec = 11;  
  19.   
  20.         static void Main(string[] args)  
  21.         {  
  22.             Program program = new Program();  
  23.             Console.WriteLine("January " + program.jan);  
  24.             Console.WriteLine("february " + program.feb);  
  25.             Console.WriteLine("March " + program.march);  
  26.             Console.WriteLine("April " + program.april);  
  27.             Console.WriteLine("May " + program.may);  
  28.             Console.WriteLine("June " + program.june);  
  29.             Console.WriteLine("July " + program.july);  
  30.             Console.WriteLine("August " + program.aug);  
  31.             Console.WriteLine("September " + program.sept);  
  32.             Console.WriteLine("October " + program.oct);  
  33.             Console.WriteLine("November " + program.nov);  
  34.             Console.WriteLine("December " + program.dec);  
  35.             Console.ReadKey();  
  36.         }  
  37.     }  
  38. }  
Output
  1. January 0  
  2. February 1  
  3. March 2  
  4. April 3  
  5. May 4  
  6. June 5  
  7. July 6  
  8. August 7  
  9. September 8  
  10. October 9  
  11. November 10  
  12. December 11  
So, here's the enum that came into the picture. It not only saves us from assigning a lot of constants but also improves the look and feel of the code, and performance which eventually makes debugging easier.
 
What is Enum?
  1. Enum is a set of named constants.
  2. To declare Enum, 'enum' keyword is used.
  3. Enum is value type
  4. Enum is byDefault Static
  5. Enum is byDefault integer type.
  6. Each symbol in enumeration list represents an integer value. Integer value increments by one as we proceed to another symbol from left to right.
  7. Int is the default type of the enumeration elements but we can change it into other data types by casting. The other data types can be byte, sbyte, short, ushort, int, unit, long, or ulong. We will discuss on this topic later on.
  8. Enum member value cannot be changed outside enum declaration. It acts as a constant.
The syntax of an enum is,
  1. enum <enum_name>   
  2.             {  
  3.             enumeration_list  
  4.             };  
Have a look at the below program to understand more. The same program is rewritten using an enum and we can observe that it made our program easy to understand and to work with.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         enum months { jan, feb, march, april, may, june, july, aug, sept, oct, nov, dec };  
  8.   
  9.         static void Main(string[] args)  
  10.         {  
  11.             Console.WriteLine("January " + months.jan + " " + (int)months.jan);  
  12.             Console.WriteLine("february " + months.feb + " " + (int)months.feb);  
  13.             Console.WriteLine("March " + months.march + " " + (int)months.march);  
  14.             Console.WriteLine("April " + months.april + " " + (int)months.april);  
  15.             Console.WriteLine("May " + months.may + " " + (int)months.may);  
  16.             Console.WriteLine("June " + months.june + " " + (int)months.june);  
  17.             Console.WriteLine("July " + months.july + " " + (int)months.july);  
  18.             Console.WriteLine("Augest " + months.aug + " " + (int)months.aug);  
  19.             Console.WriteLine("September " + months.sept + " " + (int)months.sept);  
  20.             Console.WriteLine("October " + months.oct + " " + (int)months.oct);  
  21.             Console.WriteLine("November " + months.nov + " " + (int)months.nov);  
  22.             Console.WriteLine("December " + months.dec + " " + (int)months.dec);  
  23.             Console.ReadKey();  
  24.         }  
  25.     }  
  26. }  
Output
  1. January jan 0  
  2. february feb 1  
  3. March march 2  
  4. April april 3  
  5. May may 4  
  6. June june 5  
  7. July july 6  
  8. Augest aug 7  
  9. September sept 8  
  10. October oct 9  
  11. November nov 10  
  12. December dec 11  
Here, in the program, we use 'enum' keyword to declare Enumeration 'months'. It contains a list of all the months seperated by ',' and we know that by default, the int value of the first item is 0 and it increaments by 1 for its next preceding item. So, the value of Jan is 0 and of Dec is '11'.

We can change the value by assigning that symbol with that value inside enum declaration, as shown below.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         enum months { jan, feb, march, april, may, june = 100, july, aug, sept, oct = 0, nov, dec };  
  8.   
  9.         static void Main(string[] args)  
  10.         {  
  11.             Console.WriteLine("January " + months.jan + " " + (int)months.jan);  
  12.             Console.WriteLine("february " + months.feb + " " + (int)months.feb);  
  13.             Console.WriteLine("March " + months.march + " " + (int)months.march);  
  14.             Console.WriteLine("April " + months.april + " " + (int)months.april);  
  15.             Console.WriteLine("May " + months.may + " " + (int)months.may);  
  16.             Console.WriteLine("June " + months.june + " " + (int)months.june);  
  17.             Console.WriteLine("July " + months.july + " " + (int)months.july);  
  18.             Console.WriteLine("Augest " + months.aug + " " + (int)months.aug);  
  19.             Console.WriteLine("September " + months.sept + " " + (int)months.sept);  
  20.             Console.WriteLine("October " + months.oct + " " + (int)months.oct);  
  21.             Console.WriteLine("November " + months.nov + " " + (int)months.nov);  
  22.             Console.WriteLine("December " + months.dec + " " + (int)months.dec);  
  23.             Console.ReadKey();  
  24.         }  
  25.     }  
  26. }  
Output
  1. January jan 0  
  2. february feb 1  
  3. March dec 2  
  4. April april 3  
  5. May may 4  
  6. June june 100  
  7. July july 101  
  8. Augest aug 102  
  9. September sept 103  
  10. October jan 0  
  11. November feb 1  
  12. December dec 2  
How to use different types of the enumeration elements.
 
Here is a program. The enum with 'long' type of enumeration elements is developed. We have to cast enum with 'long' followed by ':'
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         // enum is cast to long type  
  8.         enum months : long { jan, feb, march, april = 00000000001, may, june = 100, july, aug, sept, oct = 0, nov, dec = 33000000 };//InCorrect  
  9.   
  10.         static void Main(string[] args)  
  11.         {  
  12.             Console.WriteLine("January " + months.jan + " " + (long)months.jan);  
  13.             Console.WriteLine("february " + months.feb + " " + (long)months.feb);  
  14.             Console.WriteLine("March " + months.march + " " + (long)months.march);  
  15.             Console.WriteLine("April " + months.april + " " + (long)months.april);  
  16.             Console.WriteLine("May " + months.may + " " + (long)months.may);  
  17.             Console.WriteLine("June " + months.june + " " + (long)months.june);  
  18.             Console.WriteLine("July " + months.july + " " + (long)months.july);  
  19.             Console.WriteLine("Augest " + months.aug + " " + (long)months.aug);  
  20.             Console.WriteLine("September " + months.sept + " " + (long)months.sept);  
  21.             Console.WriteLine("October " + months.oct + " " + (long)months.oct);  
  22.             Console.WriteLine("November " + months.nov + " " + (long)months.nov);  
  23.             Console.WriteLine("December " + months.dec + " " + (long)months.dec);  
  24.             Console.ReadKey();  
  25.         }  
  26.     }  
  27. }  
Output
  1. January oct 0  
  2. february april 1  
  3. March may 2  
  4. April april 1  
  5. May may 2  
  6. June june 100  
  7. July july 101  
  8. Augest aug 102  
  9. September sept 103  
  10. October oct 0  
  11. November april 1  
  12. December dec 33000000  
But Enum cannot be cast into types other than byte, sbyte, short, ushort, int, unit, long, or ulong. If we cast to string, then the compile error will produce as "Type byte, sbyte, short, ushort, int, unit, long, or ulong expected".
 
What we can't do with Enum:

Null cannot be used as an item in enumeration list.
  1. enum months { jan, feb, march, april, may, june = 100, july, aug, sept, oct = 0, nov, dec,null };//InCorrect  
It will give a compile error as "Identifier expected; null is a keyword".
 
By default, the symbol value for the first item in enumeration list is 0 which we may change by assigning with a different int value. But, if we assign a value other than of type int, then the compile error will occur as shown in the below program.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         // enum of int type is declared  
  8.         // First error as Cannot implicitly convert from type 'string' to type 'int' and  
  9.         // Second error as Cannot implicitly convert from type 'double' to type 'int'. An explicit conversion exists  
  10.         enum months { jan, feb, march, april, may, june = 100, july, aug, sept, oct = "str", nov, dec = 0.333, };  
  11.   
  12.         static void Main(string[] args)  
  13.         {  
  14.             Console.WriteLine("January " + months.jan + " " + (int)months.jan);  
  15.             Console.WriteLine("february " + months.feb + " " + (int)months.feb);  
  16.             Console.WriteLine("March " + months.march + " " + (int)months.march);  
  17.             Console.WriteLine("April " + months.april + " " + (int)months.april);  
  18.             Console.WriteLine("May " + months.may + " " + (int)months.may);  
  19.             Console.WriteLine("June " + months.june + " " + (int)months.june);  
  20.             Console.WriteLine("July " + months.july + " " + (int)months.july);  
  21.             Console.WriteLine("Augest " + months.aug + " " + (int)months.aug);  
  22.             Console.WriteLine("September " + months.sept + " " + (int)months.sept);  
  23.             Console.WriteLine("October " + months.oct + " " + (int)months.oct);  
  24.             Console.WriteLine("November " + months.nov + " " + (int)months.nov);  
  25.             Console.WriteLine("December " + months.dec + " " + (int)months.dec);  
  26.             Console.ReadKey();  
  27.         }  
  28.     }  
  29. }  
Error as "Cannot implicitly convert from type 'string' to type 'int'" and "Cannot implicitly convert from type 'double' to type 'int'. An explicit conversion exists".
 
All symbols in enumeration list must be unique. No two symbols can be same in an enumeration list. But two symbols can have the same value.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         // enum of int type is declared  
  8.         enum months { jan, feb, march, april, may, june = 100, july, may, aug, sept, oct = 0, nov, dec, nov };//InCorrect  
  9.   
  10.         static void Main(string[] args)  
  11.         {  
  12.             Console.WriteLine("January " + months.jan + " " + (int)months.jan);  
  13.             Console.WriteLine("february " + months.feb + " " + (int)months.feb);  
  14.             Console.WriteLine("March " + months.march + " " + (int)months.march);  
  15.             Console.WriteLine("April " + months.april + " " + (int)months.april);  
  16.             Console.WriteLine("May " + months.may + " " + (int)months.may);  
  17.             Console.WriteLine("June " + months.june + " " + (int)months.june);  
  18.             Console.WriteLine("July " + months.july + " " + (int)months.july);  
  19.             Console.WriteLine("Augest " + months.aug + " " + (int)months.aug);  
  20.             Console.WriteLine("September " + months.sept + " " + (int)months.sept);  
  21.             Console.WriteLine("October " + months.oct + " " + (int)months.oct);  
  22.             Console.WriteLine("November " + months.nov + " " + (int)months.nov);  
  23.             Console.WriteLine("December " + months.dec + " " + (int)months.dec);  
  24.             Console.ReadKey();  
  25.         }  
  26.     }  
  27. }  
Compile Error as: "The type 'Program.months' already contains a definition for 'may'" and "The type 'Program.months' already contains a definition for 'dec'"
 
Conclusion
 
Enum makes a program easy to understand and more readable. It also improves the performance. I hope, after understanding this article, you will increase your use of  'enum' a bit more.
 
Thank you. Please feel free to ask any question or make a suggestion.


Similar Articles