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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- int jan = 0;
- int feb = 1;
- int march = 2;
- int april = 3;
- int may = 4;
- int june = 5;
- int july = 6;
- int aug = 7;
- int sept = 8;
- int oct = 9;
- int nov = 10;
- int dec = 11;
-
- static void Main(string[] args)
- {
- Program program = new Program();
- Console.WriteLine("January " + program.jan);
- Console.WriteLine("february " + program.feb);
- Console.WriteLine("March " + program.march);
- Console.WriteLine("April " + program.april);
- Console.WriteLine("May " + program.may);
- Console.WriteLine("June " + program.june);
- Console.WriteLine("July " + program.july);
- Console.WriteLine("August " + program.aug);
- Console.WriteLine("September " + program.sept);
- Console.WriteLine("October " + program.oct);
- Console.WriteLine("November " + program.nov);
- Console.WriteLine("December " + program.dec);
- Console.ReadKey();
- }
- }
- }
Output
- January 0
- February 1
- March 2
- April 3
- May 4
- June 5
- July 6
- August 7
- September 8
- October 9
- November 10
- 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?
- Enum is a set of named constants.
- To declare Enum, 'enum' keyword is used.
- Enum is value type
- Enum is byDefault Static
- Enum is byDefault integer type.
- Each symbol in enumeration list represents an integer value. Integer value increments by one as we proceed to another symbol from left to right.
- 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.
- Enum member value cannot be changed outside enum declaration. It acts as a constant.
The syntax of an enum is,
- enum <enum_name>
- {
- enumeration_list
- };
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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- enum months { jan, feb, march, april, may, june, july, aug, sept, oct, nov, dec };
-
- static void Main(string[] args)
- {
- Console.WriteLine("January " + months.jan + " " + (int)months.jan);
- Console.WriteLine("february " + months.feb + " " + (int)months.feb);
- Console.WriteLine("March " + months.march + " " + (int)months.march);
- Console.WriteLine("April " + months.april + " " + (int)months.april);
- Console.WriteLine("May " + months.may + " " + (int)months.may);
- Console.WriteLine("June " + months.june + " " + (int)months.june);
- Console.WriteLine("July " + months.july + " " + (int)months.july);
- Console.WriteLine("Augest " + months.aug + " " + (int)months.aug);
- Console.WriteLine("September " + months.sept + " " + (int)months.sept);
- Console.WriteLine("October " + months.oct + " " + (int)months.oct);
- Console.WriteLine("November " + months.nov + " " + (int)months.nov);
- Console.WriteLine("December " + months.dec + " " + (int)months.dec);
- Console.ReadKey();
- }
- }
- }
Output
- January jan 0
- february feb 1
- March march 2
- April april 3
- May may 4
- June june 5
- July july 6
- Augest aug 7
- September sept 8
- October oct 9
- November nov 10
- 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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- enum months { jan, feb, march, april, may, june = 100, july, aug, sept, oct = 0, nov, dec };
-
- static void Main(string[] args)
- {
- Console.WriteLine("January " + months.jan + " " + (int)months.jan);
- Console.WriteLine("february " + months.feb + " " + (int)months.feb);
- Console.WriteLine("March " + months.march + " " + (int)months.march);
- Console.WriteLine("April " + months.april + " " + (int)months.april);
- Console.WriteLine("May " + months.may + " " + (int)months.may);
- Console.WriteLine("June " + months.june + " " + (int)months.june);
- Console.WriteLine("July " + months.july + " " + (int)months.july);
- Console.WriteLine("Augest " + months.aug + " " + (int)months.aug);
- Console.WriteLine("September " + months.sept + " " + (int)months.sept);
- Console.WriteLine("October " + months.oct + " " + (int)months.oct);
- Console.WriteLine("November " + months.nov + " " + (int)months.nov);
- Console.WriteLine("December " + months.dec + " " + (int)months.dec);
- Console.ReadKey();
- }
- }
- }
Output
- January jan 0
- february feb 1
- March dec 2
- April april 3
- May may 4
- June june 100
- July july 101
- Augest aug 102
- September sept 103
- October jan 0
- November feb 1
- 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 ':'
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
-
- enum months : long { jan, feb, march, april = 00000000001, may, june = 100, july, aug, sept, oct = 0, nov, dec = 33000000 };
-
- static void Main(string[] args)
- {
- Console.WriteLine("January " + months.jan + " " + (long)months.jan);
- Console.WriteLine("february " + months.feb + " " + (long)months.feb);
- Console.WriteLine("March " + months.march + " " + (long)months.march);
- Console.WriteLine("April " + months.april + " " + (long)months.april);
- Console.WriteLine("May " + months.may + " " + (long)months.may);
- Console.WriteLine("June " + months.june + " " + (long)months.june);
- Console.WriteLine("July " + months.july + " " + (long)months.july);
- Console.WriteLine("Augest " + months.aug + " " + (long)months.aug);
- Console.WriteLine("September " + months.sept + " " + (long)months.sept);
- Console.WriteLine("October " + months.oct + " " + (long)months.oct);
- Console.WriteLine("November " + months.nov + " " + (long)months.nov);
- Console.WriteLine("December " + months.dec + " " + (long)months.dec);
- Console.ReadKey();
- }
- }
- }
Output
- January oct 0
- february april 1
- March may 2
- April april 1
- May may 2
- June june 100
- July july 101
- Augest aug 102
- September sept 103
- October oct 0
- November april 1
- 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.
- enum months { jan, feb, march, april, may, june = 100, july, aug, sept, oct = 0, nov, dec,null };
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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
-
-
-
- enum months { jan, feb, march, april, may, june = 100, july, aug, sept, oct = "str", nov, dec = 0.333, };
-
- static void Main(string[] args)
- {
- Console.WriteLine("January " + months.jan + " " + (int)months.jan);
- Console.WriteLine("february " + months.feb + " " + (int)months.feb);
- Console.WriteLine("March " + months.march + " " + (int)months.march);
- Console.WriteLine("April " + months.april + " " + (int)months.april);
- Console.WriteLine("May " + months.may + " " + (int)months.may);
- Console.WriteLine("June " + months.june + " " + (int)months.june);
- Console.WriteLine("July " + months.july + " " + (int)months.july);
- Console.WriteLine("Augest " + months.aug + " " + (int)months.aug);
- Console.WriteLine("September " + months.sept + " " + (int)months.sept);
- Console.WriteLine("October " + months.oct + " " + (int)months.oct);
- Console.WriteLine("November " + months.nov + " " + (int)months.nov);
- Console.WriteLine("December " + months.dec + " " + (int)months.dec);
- Console.ReadKey();
- }
- }
- }
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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
-
- enum months { jan, feb, march, april, may, june = 100, july, may, aug, sept, oct = 0, nov, dec, nov };
-
- static void Main(string[] args)
- {
- Console.WriteLine("January " + months.jan + " " + (int)months.jan);
- Console.WriteLine("february " + months.feb + " " + (int)months.feb);
- Console.WriteLine("March " + months.march + " " + (int)months.march);
- Console.WriteLine("April " + months.april + " " + (int)months.april);
- Console.WriteLine("May " + months.may + " " + (int)months.may);
- Console.WriteLine("June " + months.june + " " + (int)months.june);
- Console.WriteLine("July " + months.july + " " + (int)months.july);
- Console.WriteLine("Augest " + months.aug + " " + (int)months.aug);
- Console.WriteLine("September " + months.sept + " " + (int)months.sept);
- Console.WriteLine("October " + months.oct + " " + (int)months.oct);
- Console.WriteLine("November " + months.nov + " " + (int)months.nov);
- Console.WriteLine("December " + months.dec + " " + (int)months.dec);
- Console.ReadKey();
- }
- }
- }
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.