C# Enum or Enumerator

C# Enum is a user-defined data type that holds a list of named constant values called an Enumerator list. The C# Enum or enumerator is a value type. It is suggested to define an Enum immediately under the namespace. It can also nest inside a class or a structure. The syntax of the C# enumerator is

[<Access Modifiers>] enum <Name> [: <type>]
{
      --list of named constant values--
}

For example

public enum Technologies
{
   C, SQL, Dot Net, Java
}

Here, Access modifier is public and the Enum created with the name Technologies.

The type can be any integral types (int, short, long, uint, ulong, byte, ushort, sbyte), but the default is int.

Let us work on the C# Enum created above.

using System;

namespace CSharp_Tutorial
{
    public enum Technologies
    {
        C, SQL, DotNet, Java //by default the sequence is C=0, SQL=1, DotNet=2....
    }
    class Program
    {
        static void Main()
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Technologies t = 0; //0 is to get the first item of enum list Technologies
            t = (Technologies)1;//other than first item all the items in the list 
                                //are converted to enum type(technologies) while using
            Console.WriteLine("The 1st and 2nd items are {0}, {1}", 0, (Technologies)1);
        }
    }
}

OUTPUT

C# Enumerator 1

As we have already discussed, Enum has to define immediately under the namespace. And the code in the Main() method.

To print the first item in the C# enumerated list, Technologies, we can directly use 0. Technologies t = 0

But remaining items should be called by converting to enum type Technologies, i.e., t = (Technologies)1.

Here, t is the variable of type Technologies.

Finally, printed first and second items from the enum list Technologies.

Foregroundcolor, Backgroundcolor are the properties of Console class in the System namespace. In contrast, Consolecolor is the Enumerated List of different colors.

C# Enum or Enumerator Example

Let us see the usage of the C# Enum or Enumerator practically.

using System;

namespace CSharp_Tutorial
{
    public enum Days
    {
        Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5
    }
    class Program
    {
        public static Days MeetingDate { get; set; } = (Days)4;

        static void Main()
        {
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.WriteLine("Default date is {0} ", MeetingDate);
            MeetingDate = Days.Monday;
            Console.WriteLine("Meeting Date changed to {0} ", MeetingDate);
        }
    }
}

OUTPUT

C# Enum 2

ConsoleColor is the system defined Enum, and Days is the user-defined Enum.

Enum Days is having all business days in the list, each initialized with some user-defined number.

Here we just created a property of name MeetingDate of type Days and assigned a default value 4 to it, i.e., Default MeetingDate is Thursday.

In the Main() code of the C# Enum or enumerator example,

When we tried to change the MeetingDate, it will prompt us to select the enumerator list Days’ values. And not allow us to give value outside the list. For example, Saturday.

Note: So Enum restricts the user to choose items from the particular list supported, i.e., the user cannot select a value other than the List of Enum.