Enums in C#


enum in c# .NET

 

In this article I will explain about enum, how to create and use enum.

 

What is enum?

 

An enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined.

 

Enums type can be integer (float, int, byte, double etc.). But if you used beside int it has to be cast.

 

enum is used to create numeric constants in .NET framework. All member of enum are of enum type. There must be a numeric value for each enum type.

 

The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

 

enum Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

 

Program to demonstrate how to create and Use an Enum:

 

using System;

 

namespace example_enum

{

    class Program

    {

        public enum DayofWeek

        {

            Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

        }

 

        static void Main(string[] args)

        {

            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Sunday, DayofWeek.Sunday);

            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Monday, DayofWeek.Monday);

            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Tuesday, DayofWeek.Tuesday);

            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Wednesday, DayofWeek.Wednesday);

            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Thursday, DayofWeek.Thursday);

            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Friday, DayofWeek.Friday);

            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Saturday, DayofWeek.Saturday);

            Console.ReadLine();

        }

    }

}

 

Some points about enum: 

  • enums are enumerated data type in c#.
  • enums are not for end-user, they are meant for developers.
  • enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.
  • Enumerations (enums) make your code much more readable and understandable.
  • enum values are fixed. enum can be displayed as a string and processed as an integer.
  • The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
  • Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums.
  • Enums are value types and are created on the stack and not on the heap.

You give two same values in enum type?

 

Yes we can have same value in enum type. Example when we want to set priority options like

 

Normal                       0

Excellent                    1

Default                      0

Good                         3

 

Program showing enum type having same values

 

using System;

 

namespace enum_example4

{

    class Program

    {

        public enum DayofWeek

        {

            Sunday = 1, Monday, Tuesday = 1, Wednesday, Thursday = 2, Friday, Saturday

        }

 

 

        static void Main(string[] args)

        {

            string[] values = Enum.GetNames(typeof(DayofWeek));

            foreach (string s in values)

            {

                Console.WriteLine(s);

            }

 

            Console.WriteLine();

 

            int[] n = (int[])Enum.GetValues(typeof(DayofWeek));

            foreach (int x in n)

            {

                Console.WriteLine(x);

            }

            Console.ReadLine();

        }

    }

}

 

Program to find out number of values in enum

 

using System;

 

namespace enum_exampl3

{

    class Program

    {

        public enum DayofWeek

        {

            Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

        }

 

        static void Main(string[] args)

        {

            string[] values = Enum.GetNames(typeof(DayofWeek));

            int total = 0;

            foreach (string s in values)

            {

                Console.WriteLine(s);

                total++;

            }

            Console.WriteLine("Total values in enum type is : {0}", total);

            Console.WriteLine();

 

            int[] n = (int[])Enum.GetValues(typeof(DayofWeek));

            foreach (int x in n)

            {

                Console.WriteLine(x);

            }

            Console.ReadLine();

        }

    }

}

 

Above code review:

 

In the above program you have string and numeric representation of enum values. Enum class contains many useful methods for working with enumerations. The beauty of enum is that your can process it as integer value and display as string.

 

Hope the article would have helped you in understanding enum and their usage. Your feedback and constructive contributions are welcome.  Please feel free to contact me for feedback or comments you may have about this article.

 

Up Next
    Ebook Download
    View all
    Learn
    View all