enum keyword in c#

 


# enum keyword :- In C#,For define enumeration type we used enum keyword, an enum can assign the constant names to a group of numeric integer values.Enum is a value type data type. 

Enumeration types can be define directly inside a namespace,class, structure. 

Enum Properties:-

  • Enum used to give a name to each constant so that constant integer can be refered by its name.
  • By default, the first members of an enum have the value 0 and value of each succesive member is increased by 1.
  • Members of enum is also know as enumration list.
  • Enum contains its own value and can not inherit or can not pass to inheritance.
  • An Explicit cast is neccessary to convert from enum to integral type.   
Example:- Default Enum Values

using System; 

namespace ConsoleApplication

{

    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);           //output: Monday

            Console.WriteLine(WeekDays.Tuesday);           //output: Tuesday

            Console.WriteLine(WeekDays.Wednesday);      //output: Wednesday

            Console.WriteLine(WeekDays.Thursday);         //output: Thursday

            Console.WriteLine(WeekDays.Friday);             //output: Friday

            Console.WriteLine(WeekDays.Saturday);         //output: Saturday

            Console.WriteLine(WeekDays.Sunday);           //output: Sunday

 

        }

    }

}


Example:- Assign Values to Enum Members

using System; 

namespace ConsoleApplication

{

    class Program

    {

        enum Categories

        {

            Electronics = 1,

            Food = 5,

            Automotive = 6,

            Arts = 10,

            BeautyCare = 11,

            Fashion = 15,

            WomanFashion = 15

        } 

        static void Main(string[] args)

        {

            Console.WriteLine(Categories.Electronics);

            Console.WriteLine(Categories.Food);

            Console.WriteLine(Categories.Automotive);

            Console.WriteLine(Categories.Arts);

            Console.WriteLine(Categories.BeautyCare);

            Console.WriteLine(Categories.Fashion);

            Console.WriteLine(Categories.WomanFashion); 

        }

    }

}


Conversion:- An Explicit cast is neccessary to convert from enum to integral type.   

         using System;

namespace ConsoleApplication

{

    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.Friday); //output: Friday

            int day = (int)WeekDays.Friday; // enum to int conversion

            Console.WriteLine(day); //output: 4

 

            var wd = (WeekDays)5; // int to enum conversion

            Console.WriteLine(wd);//output: Saturday 

        }

    }

}

No comments:

Post a Comment