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
- enum Enum_Name
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine(Enum_Name.Sun);
- Console.WriteLine(Enum_Name.Mon);
- Console.WriteLine(Enum_Name.Tue);
- Console.WriteLine(Enum_Name.Wed);
- Console.WriteLine(Enum_Name.Thu);
- Console.WriteLine(Enum_Name.Fri);
- Console.WriteLine(Enum_Name.Sat);
- Console.WriteLine((int)Enum_Name.Sun);
- Console.WriteLine((int)Enum_Name.Mon);
- Console.WriteLine((int)Enum_Name.Tue);
- Console.WriteLine((int)Enum_Name.Wed);
- Console.WriteLine((int)Enum_Name.Thu);
- Console.WriteLine((int)Enum_Name.Fri);
- Console.WriteLine((int)Enum_Name.Sat);
- Console.ReadLine();
- }
Output
This example illustrates how to access the name and value of a variable of an enum.
Example 2
- enum Enum_Name
- {
- Sun, Mon, Tue, Wed = 1000, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine((int)Enum_Name.Sun);
- Console.WriteLine((int)Enum_Name.Mon);
- Console.WriteLine((int)Enum_Name.Tue);
- Console.WriteLine((int)Enum_Name.Wed);
- Console.WriteLine((int)Enum_Name.Thu);
- Console.WriteLine((int)Enum_Name.Fri);
- Console.WriteLine((int)Enum_Name.Sat);
- Console.ReadLine();
- }
OutputThis 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
- enum Enum_Name
- {
- Sun, Mon, Tue, Wed = 20000000000, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine((int)Enum_Name.Sun);
- Console.WriteLine((int)Enum_Name.Mon);
- Console.WriteLine((int)Enum_Name.Tue);
- Console.WriteLine((int)Enum_Name.Wed);
- Console.WriteLine((int)Enum_Name.Thu);
- Console.WriteLine((int)Enum_Name.Fri);
- Console.WriteLine((int)Enum_Name.Sat);
- Console.ReadLine();
- }
OutputThe 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.
- enum Enum_Name : long
- {
- Sun, Mon, Tue, Wed = 20000000000, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine((int)Enum_Name.Sun);
- Console.WriteLine((int)Enum_Name.Mon);
- Console.WriteLine((int)Enum_Name.Tue);
- Console.WriteLine((long)Enum_Name.Wed);
- Console.WriteLine((long)Enum_Name.Thu);
- Console.WriteLine((long)Enum_Name.Fri);
- Console.WriteLine((long)Enum_Name.Sat);
- Console.ReadLine();
- }
Output
Example 4
- enum Enum_Name : long
- {
- Sun = 25, Mon, Tue = Sun, Wed = Sun, Thu = Sun, Fri = Sun, Sat = Sun
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine((int)Enum_Name.Sun);
- Console.WriteLine((int)Enum_Name.Mon);
- Console.WriteLine((int)Enum_Name.Tue);
- Console.WriteLine((long)Enum_Name.Wed);
- Console.WriteLine((long)Enum_Name.Thu);
- Console.WriteLine((long)Enum_Name.Fri);
- Console.WriteLine((long)Enum_Name.Sat);
- Console.ReadLine();
- }
Output
Using an enum we can assign a value to a variable from pre-assigned variables.
Example 5
- enum Enum_Name : int
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat, Sun
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine((int)Enum_Name.Sun);
- Console.WriteLine((int)Enum_Name.Mon);
- Console.WriteLine((int)Enum_Name.Tue);
- Console.WriteLine((int)Enum_Name.Wed);
- Console.WriteLine((int)Enum_Name.Thu);
- Console.WriteLine((int)Enum_Name.Fri);
- Console.WriteLine((int)Enum_Name.Sat);
- Console.ReadLine();
- }
OutputIn an enum we cannot declare the same variable more than one time, each variable must be unique.
Example 6
- enum Enum_Name
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- String[] Str = Enum_Name.GetNames(typeof(Enum_Name));
- Array stm = Enum_Name.GetValues(typeof(Enum_Name));
- foreach (string s in Str)
- Console.WriteLine(s);
-
- foreach (int num in stm)
- Console.WriteLine(num);
-
- Console.ReadLine();
- }
OutputThis 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
- enum Enum_Name
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine(Enum_Name.Sun + Enum_Name.Thu);
- Console.ReadLine();
- }
OutputC# 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.
- enum Enum_Name
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine(((int)Enum_Name.Mon) + ( (int)Enum_Name.Thu));
- Console.WriteLine(((int)Enum_Name.Mon) - ((int)Enum_Name.Thu));
- Console.WriteLine(((int)Enum_Name.Mon) * ((int)Enum_Name.Thu));
- Console.WriteLine(((int)Enum_Name.Mon) / ((int)Enum_Name.Thu));
- Console.ReadLine();
- }
Output Example 8
- enum Enum_Name1
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- enum Enum_Name2
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- if (Enum_Name1.Sun == Enum_Name2.Sun)
- Console.WriteLine("Both Are Same");
- else
- Console.WriteLine("Both Are Not Same");
- Console.ReadLine();
- }
Output
This example explains that we cannot compare two variables of two enums.
Example 9
- enum Enum_Name
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine(Enum_Name.Sun > Enum_Name.Thu);
- Console.WriteLine(Enum_Name.Sat == Enum_Name.Sat);
- Console.WriteLine(Enum_Name.Sun < Enum_Name.Thu);
- Console.ReadLine();
- }
OutputThis example explains that we can compare two variables of the same enum.
Example 10
- enum Enum_Name
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
-
- Stack<Enum_Name> My_Stack = new Stack<Enum_Name>();
- My_Stack.Push(Enum_Name.Sun);
- My_Stack.Push(Enum_Name.Tue);
- My_Stack.Push(Enum_Name.Thu);
- My_Stack.Push(Enum_Name.Sat);
- Enum_Name nam = My_Stack.Pop();
- Enum_Name nam2 = My_Stack.Pop();
- Console.WriteLine(nam);
- Console.WriteLine(nam2);
-
- Console.ReadLine();
- }
Output
This example illustrates that we can use an enum with a collection in .Net.
Example 11
- enum Enum_Name:byte
- {
- Sun, Mon=3, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
-
- Console.WriteLine(Enum.GetUnderlyingType(typeof(Enum_Name)));
- Console.ReadLine();
- }
Output“GetUnderlyingType” is used to determine the datatype of the enum.
Example 12
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
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();
}
-
OutputThis example illustrates that enums are case sensitive. In an enum "Mon" and "mon" are two different variables.
Example 14
- enum Enum_Name:byte
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine(Enum.Equals(Enum_Name.Sun,Enum_Name.Mon));
- Console.WriteLine(Enum.Equals(Enum_Name.Mon, Enum_Name.Mon));
- Console.WriteLine(Enum.Equals(Enum_Name.Tue, Enum_Name.Mon));
-
- Console.ReadLine();
- }
Output
“Equals” is used to determine whether two objects of a specific enum are equal or not.
Example 15
- enum Enum_Name:int
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
- Console.WriteLine( Enum.Format(typeof(Enum_Name), 3, "X"));
- Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "x"));
- Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "G"));
- Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "g"));
- Console.WriteLine(Enum.Format(typeof(Enum_Name), 3,"F"));
- Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "f"));
- Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "D"));
- Console.WriteLine(Enum.Format(typeof(Enum_Name), 3, "d"));
-
- Console.ReadLine();
- }
Output“Format” converts the specific value of a specified enumerator type to its equivalent string representation depending on the specified format.
Example 16
- enum Enum_Name:int
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
-
- Console.WriteLine(Enum.IsDefined(typeof(Enum_Name),3));
- Console.WriteLine(Enum.IsDefined(typeof(Enum_Name), 6));
- Console.WriteLine(Enum.IsDefined(typeof(Enum_Name), 9));
- Console.ReadLine();
- }
Output“IsDefined” returns an indication whether a constant with a specified value exists in a specified enumeration or not.
Example 17
- enum Enum_Name:int
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
-
-
- Console.WriteLine(Enum.ToObject(typeof(Enum_Name), 2));
- Console.WriteLine(Enum.ToObject(typeof(Enum_Name), 5));
- Console.ReadLine();
- }
Output“ToObject” converts the specified 32-bit long signed integer to an enumerator member.
Example 18
- enum Enum_Name:int
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
-
-
- Enum_Name Nm2;
-
- Nm2 = (Enum_Name)Enum.Parse(typeof(Enum_Name), "tue",true);
-
- if (Nm2 == Enum_Name.Tue)
- Console.WriteLine("Both Are Same");
-
- Console.ReadLine();
- }
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
- enum Enum_Name:int
- {
- Sun, Mon, Tue, Wed, Thu, Fri, Sat
- };
-
- static void Main(string[] args)
- {
-
-
- Enum_Name Nm2;
-
- try
- {
- Nm2 = (Enum_Name)Enum.Parse(typeof(Enum_Name), "tue", false);
-
- if (Nm2 == Enum_Name.Tue)
- Console.WriteLine("Both Are Same");
- }
- catch(Exception Exp)
- {
- Console.WriteLine("Error Occur");
- Console.WriteLine(Exp.ToString());
- }
-
- Console.ReadLine();
- }
OutputThis 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.
- [Flags]
- enum Enum_Name:int
- {
- Sun=1,
- Mon=2,
- Tue=4,
- Wed=8,
- Thu=16,
- Fri=32,
- Sat=64
- };
-
-
- static void Main(string[] args)
- {
-
-
- Enum_Name Nm2;
-
- Enum_Name Enm;
- Enm = Enum_Name.Sun | Enum_Name.Thu | Enum_Name.Sat;
- Console.WriteLine(Enm);
- Console.ReadLine();
- }
OutputNote: 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:
- 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.
- 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.
- enum Enum_Name:int
- {
- Sun=1,
- Mon=2,
- Tue=4,
- Wed=8,
- Thu=16,
- Fri=32,
- Sat=64
- };
-
-
- static void Main(string[] args)
- {
-
-
- Enum_Name Nm2;
-
- Enum_Name Enm;
- Enm = Enum_Name.Mon | Enum_Name.Wed | Enum_Name.Sat;
- Enum_Name Check = Enum_Name.Mon;
-
- if ((Enm & Enum_Name.Mon) == Check)
- Console.WriteLine("Employee Must Be in Formal Dress Code");
- else
- Console.WriteLine("Employee May Be in Formal Dress Code");
-
- Check = Enum_Name.Thu;
-
- if ((Enm & Enum_Name.Mon) == Check)
- Console.WriteLine("Employee Must Be in Formal Dress Code");
- else
- Console.WriteLine("Employee May Be in Formal Dress Code");
-
- Console.ReadLine();
- }
Output