Understanding Enums Constants in C#

Introduction

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 make 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 define an enumerated data type.

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

Syntax

enum Enum_Name {
    Value1,
    Value2,
    Value3,
    // Add more values as needed...
    ValueN
};

Example 1

using System;
enum Enum_Name
{
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
}
class Program
{
    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

Name

This example illustrates how to access the name and value of a variable of an enum.

Example 2

using System;
enum Enum_Name
{
    Sun,
    Mon,
    Tue,
    Wed = 1000,
    Thu,
    Fri,
    Sat
}
class Program
{
    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();
    }
}

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

using System;
enum Enum_Name
{
    Sun,
    Mon,
    Tue,
    Wed = 20000000000,
    Thu,
    Fri,
    Sat
}
class Program
{
    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();
    }
}

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.

using System;
enum Enum_Name : long
{
    Sun,
    Mon,
    Tue,
    Wed = 20000000000,
    Thu,
    Fri,
    Sat
}
class Program
{
    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

long

Example 4

using System;
enum Enum_Name : long
{
    Sun = 25,
    Mon,
    Tue = Sun,
    Wed = Sun,
    Thu = Sun,
    Fri = Sun,
    Sat = Sun
}
class Program
{
    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

assign a value

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

Example 5

using System;
enum Enum_Name : int
{
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat,
    Sun // Duplicate value is allowed, but it will not affect the enumeration
}
class Program
{
    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();
    }
}

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

using System;
enum Enum_Name
{
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
}
class Program
{
    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();
    }
}

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

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();
}

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.

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

Enum

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

compare

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();
}

Output

compare two variable

This 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

illustrate

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

"GetUnderlyingType" is used to determine the data type 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 cannot be inherited");
    }
}
static void Main(string[] args)
{
    My_class Obj = new My_class();
    Enum_Name Enm = Enum_Name.Mon;
    Console.ReadLine();
}

Output

An enum cannot be inherited because an enum is a "Sealed" type.

Example 13

using System;
enum EnumName
{
    Sun, Mon, Tue, Wed, Thu, Fri, Sat, MonCaseInsensitive
}
class Program
{
    static void Main(string[] args)
    {
        if (EnumName.Mon == EnumName.MonCaseInsensitive)
            Console.WriteLine("Enums are Case Insensitive");
        else
            Console.WriteLine("Enums are Case Sensitive");

        Console.ReadLine();
    }
}

Output

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

Example 14

using System;
enum EnumName : byte
{
    Sun, Mon, Tue, Wed, Thu, Fri, Sat
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Enum.Equals(EnumName.Sun, EnumName.Mon));
        Console.WriteLine(Enum.Equals(EnumName.Mon, EnumName.Mon));
        Console.WriteLine(Enum.Equals(EnumName.Tue, EnumName.Mon));
        Console.ReadLine();
    }
}

Output

Equals

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

Example 15

using System;
enum EnumName : int
{
    Sun, Mon, Tue, Wed, Thu, Fri, Sat
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Enum.Format(typeof(EnumName), 3, "X"));
        Console.WriteLine(Enum.Format(typeof(EnumName), 3, "x"));
        Console.WriteLine(Enum.Format(typeof(EnumName), 3, "G"));
        Console.WriteLine(Enum.Format(typeof(EnumName), 3, "g"));
        Console.WriteLine(Enum.Format(typeof(EnumName), 3, "F"));
        Console.WriteLine(Enum.Format(typeof(EnumName), 3, "f"));
        Console.WriteLine(Enum.Format(typeof(EnumName), 3, "D"));
        Console.WriteLine(Enum.Format(typeof(EnumName), 3, "d"));
        Console.ReadLine();
    }
}

Output

Format

"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
}
class Program
{
    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

GetValues

"IsDefined" returns an indication of 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
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Enum.ToObject(typeof(Enum_Name), 2));
        Console.WriteLine(Enum.ToObject(typeof(Enum_Name), 5));
        Console.ReadLine();
    }
}

Output

tru

"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
}
class Program
{
    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();
    }
}

Let us see an example of case sensitivity.

Example 19

enum Enum_Name : int
{
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
}
class Program
{
    static void Main(string[] args)
    {
        Enum_Name Nm2;
        try
        {
            Nm2 = (Enum_Name)Enum.Parse(typeof(Enum_Name), "Tue", ignoreCase: true);

            if (Nm2 == Enum_Name.Tue)
                Console.WriteLine("Both are the same");
        }
        catch (Exception Exp)
        {
            Console.WriteLine("An error occurred");
            Console.WriteLine(Exp.ToString());
        }
        Console.ReadLine();
    }
}

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 didn't explain "Enum Flags".

What is a "Flag Enum"?

 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
}
class Program
{
    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();
    }
}

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 Saturday, the employee must be in a 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 the week in which employees must be a 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.
    using System;
    enum Enum_Name : int
    {
        Sun = 1,
        Mon = 2,
        Tue = 4,
        Wed = 8,
        Thu = 16,
        Fri = 32,
        Sat = 64
    }
    class Program
    {
        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

output


Similar Articles