enum in C# with Example

Enum used to declare enumeration, by default first enumerator is set as Zero and increment by 1. Enums can be easily converted to integer values or strings. This tutorial also shows how create a foreach loop for the enum variables.


In the below tutorial we have implemented to create the number for each month that have declared in the  enum.




C# CODE

using System;

namespace enumsample
{
    class Program
    {
        enum Month
        {
            Jan, Feb, Mar,
            Apr, May, Jun,
            Jul, Aug, Sep,
            Oct, Nov, Dec
        };

        static void Main(string[] args)
        {
            foreach (Month months in Enum.GetValues(typeof(Month)))
            {
                int x = (int)months;
                Console.WriteLine(months + "={0}", x + 1);
            }
            Console.ReadLine();
        }
    }
}