Understanding Enums Constants in C#

Enums are known as named constants. If we have some constants related to each other then we can use an enum to group all the constants. Enums are strongly-typed constants that 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. An enumeration is a user-defined data type consisting of integral constants and each integral constant is given a name. The keyword enum is used to defined an enumerated data type.

Enum is a very easy and interesting topic. Today we learn some new things about enums.

Syntax

enum Enum_Name ( Value1,Value2,Value3,……………………ValueN)

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

Name
 
This example illustrates how to access the name and value of a variable of an enum.
 
Example 2
  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed = 1000, 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

previous value

This example illustrates that if we assign an integer value to an enum variable then the next variable will contain a value just greater than 1 from the previous value.

Example 3
  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed = 20000000000, 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

program output

The preceding program will throw an error because, by default, the data type of an enum is an integer. In the preceding example we exceed the range of integers so it throws an error. To remove this error we type-cast the enum.
  1. enum Enum_Name : long  
  2. {  
  3.    Sun, Mon, Tue, Wed = 20000000000, 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

long

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

assign a value
Using an enum we can assign a value to a variable from pre-assigned variables.

Example 5
  1. enum Enum_Name : int  
  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((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

variable must be unique

In an enum we cannot declare the same variable more than one time, each variable must be unique.

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.     Array stm = Enum_Name.GetValues(typeof(Enum_Name));  
  10.     foreach (string s in Str)  
  11.         Console.WriteLine(s);  
  12.   
  13.     foreach (int  num in stm)  
  14.         Console.WriteLine(num);  
  15.   
  16.     Console.ReadLine();  
  17. }    
Output

Enum using a loop

This example illustrates how to access a variable name and the values of an enum using a loop. This approach is very helpful when our enum contains a large number of variables.

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

arithmetic operation

C# doesn't allow us to do arithmetic operations on two members of an enum as shown in the preceding program. In order to do so we need to overload the arithmetic operator.

  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.Mon) + ( (int)Enum_Name.Thu));  
  9.    Console.WriteLine(((int)Enum_Name.Mon) - ((int)Enum_Name.Thu));  
  10.    Console.WriteLine(((int)Enum_Name.Mon) * ((int)Enum_Name.Thu));  
  11.    Console.WriteLine(((int)Enum_Name.Mon) / ((int)Enum_Name.Thu));  
  12.    Console.ReadLine();  
  13. }   
Output

Enum
Example 8
  1. enum Enum_Name1  
  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.     if (Enum_Name1.Sun == Enum_Name2.Sun)  
  14.         Console.WriteLine("Both Are Same");  
  15.     else  
  16.         Console.WriteLine("Both Are Not Same");  
  17.     Console.ReadLine();  
  18. }    
Output

compare

This example explains that we cannot  compare two variables of two enums.

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

compare two variable

This example explains that we can compare two variables of the same enum.

Example 10
  1. enum Enum_Name  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.   
  9.     Stack<Enum_Name> My_Stack = new Stack<Enum_Name>();  
  10.     My_Stack.Push(Enum_Name.Sun);  
  11.     My_Stack.Push(Enum_Name.Tue);  
  12.     My_Stack.Push(Enum_Name.Thu);  
  13.     My_Stack.Push(Enum_Name.Sat);  
  14.     Enum_Name nam = My_Stack.Pop();  
  15.     Enum_Name nam2 = My_Stack.Pop();  
  16.     Console.WriteLine(nam);  
  17.     Console.WriteLine(nam2);  
  18.   
  19.     Console.ReadLine();  
  20. }  
Output

illustrate

This example illustrates that we can use an enum with a collection in .Net.

Example 11
  1. enum Enum_Name:byte  
  2. {  
  3.    Sun, Mon=3, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.   
  9.    Console.WriteLine(Enum.GetUnderlyingType(typeof(Enum_Name)));  
  10.    Console.ReadLine();  
  11. }  
Output

GetUnderlyingType

“GetUnderlyingType” is used to determine the datatype of the enum.

Example 12

  1. enum Enum_Name
    {
    Sun, Mon, Tue, Wed, Thu, Fri, Sat
    };


    class My_class : Enum_Name
    {
    public void Call()
    {
    Console.WriteLine("Enum Can not be inherit ");
    }
    }
    static void Main(string[] args)
    {
    My_class Obj = new My_class();
    Enum_Name Enm = Enum_Name.Mon;

    Console.ReadLine();
    }

Output



An enum cannot be inherit because a enum is  a "Sealed" type.

Example 13

  1. enum Enum_Name
    {

    Sun, Mon, Tue, Wed, Thu, Fri, Sat ,mon
    };

    static void Main(string[] args)
    {
    if (Enum_Name.Mon == Enum_Name.mon)
    Console.WriteLine("Enum are Case Insensitive");
    else
    Console.WriteLine("Enum are Case Sensitive");
    Console.ReadLine();
    }


  2.   
Output



This example illustrates that enums are case sensitive. In an enum "Mon" and "mon" are two different variables.

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

Equals

“Equals” is used to determine whether two objects of a specific enum are equal or not.

Example 15
  1. enum Enum_Name:int  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.    Console.WriteLine( Enum.Format(typeof(Enum_Name), 3, "X"));  
  9.    Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "x"));  
  10.    Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "G"));  
  11.    Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "g"));  
  12.    Console.WriteLine(Enum.Format(typeof(Enum_Name), 3,"F"));  
  13.    Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "f"));  
  14.    Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "D"));  
  15.    Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "d"));  
  16.       
  17.     Console.ReadLine();  
  18. }    
Output

Format

“Format” converts the specific value of a specified enumerator type to its equivalent string representation depending on the specified format.

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

GetValues

“IsDefined” returns an indication whether a constant with a specified value exists in a specified enumeration or not.

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

tru

“ToObject” converts the specified 32-bit long signed integer to an enumerator member.

Example 18

  1. enum Enum_Name:int  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.   
  9.    
  10.     Enum_Name Nm2;  
  11.   
  12.     Nm2 = (Enum_Name)Enum.Parse(typeof(Enum_Name), "tue",true);  
  13.       
  14.     if (Nm2 == Enum_Name.Tue)  
  15.         Console.WriteLine("Both Are Same");  
  16.   
  17.     Console.ReadLine();  
  18. }    
Output

Both Are the Same

“Pasre” converts the string representation of the name of a numeric value to one or more enumerated constants to an equivalent object. A parameter specifies whether the operation is case- insensitive.
In this example “true” indicates that the operation is case insensitive .

Let us see an example of case sensitivity.

Example 19
  1. enum Enum_Name:int  
  2. {  
  3.     Sun, Mon, Tue, Wed, Thu, Fri, Sat  
  4. };  
  5.   
  6. static void Main(string[] args)  
  7. {  
  8.   
  9.    
  10.     Enum_Name Nm2;  
  11.   
  12.     try  
  13.     {  
  14.         Nm2 = (Enum_Name)Enum.Parse(typeof(Enum_Name), "tue"false);  
  15.   
  16.         if (Nm2 == Enum_Name.Tue)  
  17.             Console.WriteLine("Both Are Same");  
  18.     }  
  19.     catch(Exception Exp)  
  20.     {  
  21.         Console.WriteLine("Error Occur");  
  22.         Console.WriteLine(Exp.ToString());  
  23.     }  
  24.   
  25.     Console.ReadLine();  
  26. }    
Output

example of case sensitive

This example illustrates that if we set the parameters equal to “False” then the operation is treated as case-sensitive and if an object is not found in the enumerator then it will throw an error.

Example 20

I think this article would be incomplete if I did't explain “Enum Flags”.

What is a “Flag Enum”?

Answer: A Flag Enum creates an enum variable such that it can contain multiple values.

Let us see an example.
  1. [Flags]  
  2. enum Enum_Name:int  
  3. {  
  4.     Sun=1,  
  5.     Mon=2,  
  6.     Tue=4,  
  7.     Wed=8,  
  8.     Thu=16,  
  9.     Fri=32,  
  10.     Sat=64  
  11. };  
  12.   
  13.   
  14. static void Main(string[] args)  
  15. {  
  16.   
  17.    
  18.     Enum_Name Nm2;  
  19.   
  20.     Enum_Name Enm;  
  21.     Enm = Enum_Name.Sun | Enum_Name.Thu | Enum_Name.Sat;  
  22.     Console.WriteLine(Enm);  
  23.     Console.ReadLine();  
  24. }    
Output

Flag Enum

Note: The value of an enumerator variable should be increased by a power of two.

Now a question arises of what is the use or benefits of storing multiple values in a single enumerator variable. Assume that you are working in a company and there is a rule that for each Monday, Wednesday and Saturday the employee must be in formal dress code. Then how can remember which day of the week the employee should be in the formal dress code?

There are the following two approaches:
  1. Create an array and store the day of week in which employee must be formal dress code. This is not an appropriate way, because it takes greater space if we create an array and also takes time to determine the day.
  2. The second approach is to use a “Flag Enum” that is more efficient compared to the first approach. Let us see how it will work. 
  1. enum Enum_Name:int  
  2. {  
  3.     Sun=1,  
  4.     Mon=2,  
  5.     Tue=4,  
  6.     Wed=8,  
  7.     Thu=16,  
  8.     Fri=32,  
  9.     Sat=64  
  10. };  
  11.   
  12.   
  13. static void Main(string[] args)  
  14. {  
  15.   
  16.    
  17.     Enum_Name Nm2;  
  18.   
  19.     Enum_Name Enm;  
  20.     Enm = Enum_Name.Mon | Enum_Name.Wed | Enum_Name.Sat;  
  21.     Enum_Name Check = Enum_Name.Mon;  
  22.   
  23.     if ((Enm & Enum_Name.Mon) == Check)  
  24.         Console.WriteLine("Employee Must Be in Formal Dress Code");  
  25.     else  
  26.         Console.WriteLine("Employee May Be in Formal Dress Code");  
  27.   
  28.     Check = Enum_Name.Thu;  
  29.   
  30.     if ((Enm & Enum_Name.Mon) == Check)  
  31.         Console.WriteLine("Employee Must Be in Formal Dress Code");  
  32.     else  
  33.         Console.WriteLine("Employee May Be in Formal Dress Code");  
  34.       
  35.     Console.ReadLine();  
  36. }    
Output

output

 

Up Next
    Ebook Download
    View all
    Learn
    View all