Giving an Enum a String Value Using the Description Attribute

Published Mar 27, 2017
Giving an Enum a String Value Using the Description Attribute

When working with enums in C#, it is sometimes necessary to get a string description of the value associated with the enum. This can be accomplished easily by using the DescriptionAttribute class.

Originally, I was using code like the following to set the string value based on the value of an enum, but it seemed too cumbersome and had to be used everytime I wanted to parse an enum.

string myString = null;
switch(myEnum) 
{
    case myEnum.Value1: 
        myString = "value 1"; 
        break; 
    case myEnum.Value2: 
        myString = "value 2"; 
        break; 
    case myEnum.Value3: 
        myString = "value 3"; 
        break; 
}

I wanted to do something simpler. So, I decided to add the System.ComponentModel.DescriptionAttribute attribute to my enum, like so:

using System.ComponentModel; 

public enum MyEnum 
{ 
    [Description("value 1")] 
    Value1, 
    [Description("value 2")]
    Value2, 
    [Description("value 3")]
    Value3
}

Then, to make it easy to use, I added an extension method called GetDescription() to all enums:

public static string GetDescription<T>(this T e) where T : IConvertible
{
    string description = null;

    if (e is Enum)
    {
        Type type = e.GetType();
        Array values = System.Enum.GetValues(type);

        foreach (int val in values)
        {
            if (val == e.ToInt32(CultureInfo.InvariantCulture))
            {
                var memInfo = type.GetMember(type.GetEnumName(val));
                var descriptionAttributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                if(descriptionAttributes.Length > 0)
                {
                    // we're only getting the first description we find
                    // others will be ignored
                    description = ((DescriptionAttribute)descriptionAttributes[0]).Description;
                }

                break;
            }
        }
    }

    return description;
}

This will allow you to get the description of the current enum value easily. Here is how you would do that:

MyEnum testEnum = MyEnum.Value2;
Debug.WriteLine(testEnum.GetDescription()); // output will be "value 2"

As you can see, it’s pretty easy to decorate your enum values with string descriptions. This allows you to use a central location to set the value.

I can even see a custom description attribute class that retrieves the description from a database. The use cases are there.

Another more common use case for this method is to localize the values returned by using resource files, representing the languages you want to display. You could have one for en-US, en-GB, es-ES, es-MX, etc., using the description Name as the key to the resource file.

Discover and read more posts from Michael Earls
get started
Enjoy this post?

Leave a like and comment for Michael

1
Be the first to share your opinion

Get curated posts in your inbox

Read more posts to become a better developer