C# - Read Enum Description Attribute

We will use Enumerations when we expect some value from the list of values. For example, if we want to display the data from available sources based on user selection. We can define available sources in enumeration and can compare with user input. In this article, we discuss how to define enumeration and how to read description attribute from enumeration in C#. In C#, we can define enumeration by using enum keyword.

Open Microsoft Visual Studio 2015 => Create new Console Application and name it as ReadEnumCSharp. Create enumeration EnumSources as shown below by using enum keyword.

public enum EnumSources

{

        XMLSource = 0,

        ExcelSource = 1,

        MSSQLSource = 2,

        OracleSource = 3

}

As shown above, we have created the EnumSources enumeration by using enum keyword and also we indexed with the enum elements with numbers. It’s not mandatory to assign numbers in order, you can assign random numbers to enumeration values but those should be unique. You can compare the enum values by using if condition as shown below.

           //here assign value based on user input

            var enumSource;

            if (enumSource == EnumSources.ExcelSource)

            { 

            }

We cannot have multiple words as enum value like we cannot have “XML Source” as an enum value. But it is very useful to have a meaningful description for each enum value. We can achieve this by applying Description attribute for each enum value as shown below. The description attribute available in System.ComponentModel namespace.

public enum EnumSources

{

        [Description("XML Data Source")]

        XMLSource = 0, 

        [Description("Excel Data Source")]

        ExcelSource = 1, 

        [Description("SQL Data Source")]

        MSSQLSource = 2, 

        [Description("Oracle Data Source")]

        OracleSource = 3

   }

Sometimes we might have to bind enum with drop down list or Gridview or any other control. In this scenario it is better to display enum description which provides some meaningful information. We can read enum value description by using Reflection mechanism. Let’s create Enum Helper class and GetEnumDescription() method as shown below.

using System;

using System.ComponentModel; 

namespace ReadEnumCSharp

{

    public static class EnumHelper

    {

        public static string GetEnumDescription(this Enum value)

        {

            var enumType = value.GetType();

             var field = enumType.GetField(value.ToString());

            var attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false);

            return attributes.Length == 0 ? value.ToString() : ((DescriptionAttribute)attributes[0]).Description;

        }

    }

}

 

Call GetEnumDescription() method to get enum value description by passing enum values as shown below.

using System;

using System.ComponentModel; 

namespace ReadEnumCSharp

{

    class Program

    {

        static void Main(string[] args)

        {

            foreach (var source in Enum.GetValues(typeof(EnumSources)))

            {

                Console.WriteLine(EnumHelper.GetEnumDescription((EnumSources)source));

            }

            Console.ReadLine();

        }

    } 

    public enum EnumSources

    {

        [Description("XML Data Source")]

        XMLSource = 0, 

        [Description("Excel Data Source")]

        ExcelSource = 1, 

        [Description("SQL Data Source")]

        MSSQLSource = 2, 

        [Description("Oracle Data Source")]

        OracleSource = 3

    }

}

 

If you run the application, it displays enum value description as shown below.

                                                                                                                                              ReadEnumCSharp.zip