Update – Check out this article for the latest and greatest.

I’ve decided to post an EnumerationHelpers class written in C# 2.0. This class allows you to easily work with description tags on your enumeration values, and search them to pull the appropriate value out based on the description. This is great for times like when you want an enumeration to control a picklist but you don’t want to have to create a switch statement to match the displayed value with the enumeration value. In that respect, it also effectively isolates you from change (but, displayed values never change… right…). Here is an example of how to use it:

enum Status
{
 [Description("This is a new item")] New,
 [Description("This is an existing item")] Existing,
 [Description("This is an old item")] Old,
 DescriptionUnavailable
};

Status s = Status.New;

// Returns "This is a new item"
string strDescription = EnumerationHelpers.GetEnumDescription(s);

// Returns Status.Existing
s = EnumerationHelpers.GetEnumValueFromDescription("This is an existing item");

// Returns Status.Old
s = EnumerationHelpers.GetEnumValueFromDescription("Old");

// Returns "DescriptionUnavailable"
strDescription = EnumerationHelpers.GetEnumDescription(Status.DescriptionUnavailable);

As you can see, it becomes a fairly trivial task to implement describable enumerations. You can also see that it relies on the power of generics, and is type-safe. Feel free to take a look under the hood, and to use it for whatever needs you have. All I ask is that you leave in the copyright on the top of the code. Included in the package is a .csproj which includes the helper class and a set of NUnit tests that can help show you the ropes with the class. It’s available here!