C# Enum (Enumerator)

  Keywords : c# enum to int, c# int to enum, c# enum example, c# enum string, c# enum int, c# enum to string, c# string to enum, c# enum string value

Here we will learn what is enum in c#, how to use c# enum to declare an enumerator, c# enum with integer types, enum methods in c# and iterate or loop through enum in c# examples.

C# Enum

In c#, enum is a keyword which is used to declare an enumeration. In c#, enumeration is a type that consists a set of named constants as list.

 

By using an enumeration, we can group a constants that are logically related to each other. For example, the days of week can be grouped together by using enumeration in c#.

C# Enum Syntax

Following is the syntax of defining an enumeration using enum keyword in c# programming language.

 

enum enum_name

{

// enumeration list

}

If you observe above syntax, we used enum keyword to define an enumeration based on our requirements.

 

Following is the example of defining an enumeration using enum keyword in c# programming language.

 

enum Week

{

    Sunday,

    Monday,

    Tuesday,

    Wednesday,

    Thursday,

    Friday,

    Saturday

}

If you observe above example, we define an enumeration “Week” with a list of named constants called enumeration list.

 

In c#, by default the first named constant in enumerator has a value of 0, and the value of each successive item in enumerator will be increased by 1. For example, in above enumeration, Sunday value is 0, Monday is 1, Tuesday is 2, and so forth.

 

In case, if we want to change the default values of an enumerator, then assigning a new value to the first item in enumerator will automatically assign an incremental values to the successive items in an enumerator.

 

Following is the example of overriding the default values of an enumerator by assigning a new value to the first item in an enumerator.

 

enum Week

  {

      Sunday = 10,

      Monday,

      Tuesday,

      Wednesday,

      Thursday,

      Friday,

      Saturday

  }

In above enumeration, the sequence of elements are forced to start from 10 instead of 0 like Sunday value is 10, Monday is 11, Tuesday is 12, and so forth.

C# Enum with Integer Types

In c#, an enumeration can contain only integral data type items such as byte, sbyte, short, ushort, int, uint, long or ulong. It’s not possible to use enum with string or any other data types to define an enum elements except numeric types.

 

The default type of enumeration elements is int (integer). In case, if we want to change the integral type of enum to byte, then we need mention a byte type with colon (:) after the identifier like as shown below.

 

enum Week : byte

  {

      Sunday,

      Monday,

      Tuesday,

      Wednesday,

      Thursday,

      Friday,

      Saturday

  }

If you observe above example, we are trying to change the default integral type of elements in an enumeration to byte type.

 

To get the values of enum elements in c#, an explicit cast is necessary to convert from enum type to an integral type.

 

For example, following are the statements to get an enum item value by using a cast to convert from enum to int.

 

int a = (int)Week.Sunday; // It will return 0

int b = (int)Week.Monday; // It will return 1

If you observe above statements, we are doing cast conversion (enum to int) to get an enum item values.

C# Enum Example

Following is the example of declaring an enumeration using enum keyword in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        enum Week

        {

            Sunday,

            Monday,

            Tuesday,

            Wednesday,

            Thursday,

            Friday,

            Saturday

        }

        static void Main(string[] args)

        {

            int a = (int)Week.Sunday;

            int b = (int)Week.Monday;

            int c = (int)Week.Tuesday;

            Console.WriteLine(Week.Sunday);

            Console.WriteLine(Week.Monday);

            Console.WriteLine("Sunday: {0}", a);

            Console.WriteLine("Monday: {0}", b);

            Console.WriteLine("Tuesday: {0}", c);

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we defined an enumeration Week and getting the values of enumeration items by explicitly converting from enum to int and assigned to an integer variables.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Enum or Enumerator Example Result

 

If you observe above result, the first enumerator (Sunday) has a value of 0 and the value of each successive enumerator is increased by 1.

C# Enum Methods

In c#, we have a class called Enum that contains a following helper methods to work with an enumeration (enum).

 

MethodDescription
Format It is used to convert the value of enum type to specified string format.
GetName It is used to get the name of specified enum item.
GetNames It is used to get all item names of specified enum as an array.
GetValues It is used to get all item values of specified enum as an array.
Parse It is used to convert the string representation of name or numeric value of one or more enumerated constants to an equivalent enumerated object.
GetUnderlyingType It is used return the underlying type of specified enumeration.

C# Iterate through Enum

In c#, we can iterate or loop through an enum items using for or foreach loop to get the enumeration item names or values by using enum helper methods.

 

Following is the example of loop through an enum items to get all item names and values using enum helper methods based on our requirements.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        enum Week

        {

            Sunday,

            Monday,

            Tuesday,

            Wednesday,

            Thursday,

            Friday,

            Saturday

        }

        static void Main(string[] args)

        {

            Console.WriteLine("Week Enumeration Values");

            foreach (string w in Enum.GetNames(typeof(Week)))

            {

                Console.WriteLine(w);

            }

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we are looping through an enumeration using foreach loop and getting all item names using GetNames() helper method.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Iterate through Enum Example Result

 

If you observe above result, we are able to get all enumeration item names by looping through an enumeration using foreach loop based on our requirements.

 

In c#, it’s a better to define an enum within namespace so that all the classes in the namespace can access equally and we can also nested an enum within class or struct based on our requirements.