Enum in C#

enum is a value type data type.The Enum is used to declare a list of named integer constants.it can be used to define enum keyword directly inside a class or namespace. The enum is used to give a name to each constant. So that the constant integer can be referred using its name.

enum example:

enum Weekdays
{
    Monday = 0,
    Tuesday =1,
    Wednesday = 2,
    Thursday = 3,
    Friday = 4,
    Saturday =5,
    Sunday = 6

Example:

namespace Enum
{
    class Program
    {
        enum WeekDays
        {
            Monday = 0,
            Tuesday = 1,
            Wednesday = 2,
            Thursday = 3,
            Friday = 4,
            Saturday = 5,
            Sunday = 6
        }
        static void Main(string[] args)
        {
            Console.WriteLine(WeekDays.Monday);
            Console.WriteLine((int)WeekDays.Monday);
            Console.ReadLine();
        }
    }
}

Explonation:

Console.WriteLine(WeekDays.Monday);
Console.WriteLine((int)WeekDays.Monday);

By default,The first mumber of an enum is 0 and the value of each successive enum  is incremented by 1.

int dayNum = (int)WeekDays.Monday;

An Explicit cast is required to convert from enum type to integer type.


  1. The enum is a set of named constant.
  2. The value of enum constants starts from 0. Enum can have value of any valid numeric type.
  3. String enum is not supported in C#.
  4. Use of enum makes code more readable and manageable.

Popular Posts