X

EnumHelper class

Some time ago I wrote blog posting Extension method for enumerators where I showed how to use extension methods to make using of enumerators more convenient. Today I found very interesting posting from Grant Barrington blog where he describes the class called EnumHelper.

As enumerator values are often used on web forms as dropdown lists then we need a way to show nice options to users. Something like BrightPink or MyHomeAddress may confuse them. Grant offers us a nice solution – we can use description attributes to bind user readable values to enumerator members.

Take a look at the following code sample I took from Grant’s blog. It is the best illustration about his work.

using System.ComponentModel;

namespace Ellington.EnumHelperExamples
{
    public enum UserColours
    {
        [Description("Burnt Orange")]
        BurntOrange = 1,

        [Description("Bright Pink")]
        BrightPink = 2,

        [Description("Dark Green")]
        DarkGreen = 3,

        [Description("Sky Blue")]
        SkyBlue = 4
    }
}

It seems cool to me. Thanks Grant!

Liked this post? Empower your friends by sharing it!
Categories: C#
Related Post