What is Enum in C#

What is Enum in C#

Enum is a value type data type.
Enum is used to declare list of named integer constant.
It is defined from base class System.Enum

To create an enum, use the enum keyword and use comma ( , ) to separate the enum items. 

enum VideoQuality { SD, HD, UHD }

Enum is short for "enumerations", which means "specifically listed".

When To Use Enums? 

If we have values that we know will remain same, like months, days, colors etc.

We can also use enum inside a class:


Example:

 class Program

    {

        enum weekDays

        {

            Monday,

            Tuesday,

            Wednesday,

            Thursday,

            Friday,

            Saturday,

            Sunday

        }

 

        static void Main(string[] args)

        {

 

            Console.WriteLine(weekDays.Friday);

            Console.WriteLine((int)weekDays.Friday);

 

        }

    }

OUTPUT:



Notes:

By default, the value of first constant of Enum is 0.

Every subsequent value is increased by 1.

if we change the value of any constant, other constant will be adjust by value increment.

Post a Comment

0 Comments