Tips For Working With Enums

If you use enums frequently throughout code, you may be interested in the following code snippets. The first, is a fairly common extension method I’ve seen for getting descriptions from enum values (this is not my code and unfortunately, I can only site Google as the source).

/// <summary>
/// A collection of extension methods for enums.
/// </summary>
/// <typeparam name="T"></typeparam>
internal static class Enum<T>
{
	/// <summary>
	/// An extension method for enums to get the textual description of a specified enum value.
	/// </summary>
	/// <param name="value"></param>
	/// <returns></returns>
	internal static string GetDescription(T value)
	{
		var da = (DescriptionAttribute[])(typeof(T).GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false));
		return da.Length > 0 ? da[0].Description : value.ToString();
	}

	/// <summary>
	/// An extension method for enums to get the enum by textual description.
	/// </summary>
	/// <param name="value"></param>
	/// <param name="description"></param>
	/// <returns></returns>
	internal static string GetEnumName(Type value, string description)
	{
		FieldInfo[] fis = value.GetFields();
		foreach (FieldInfo fi in fis)
		{
			DescriptionAttribute[] attributes =
			  (DescriptionAttribute[])fi.GetCustomAttributes
			  (typeof(DescriptionAttribute), false);
			if (attributes.Length > 0)
			{
				if (attributes[0].Description == description)
				{
					return fi.Name;
				}
			}
		}
		return description;
	}
}

Samples

Let’s begin by setting up a very basic enum called MyEnum. Don’t forget to add using System.ComponentModel; to your using statements.

public enum MyEnum
{
	[Description("Red")]
	Red = 1,

	[Description("Blue")]
	Blue = 2,

	[Description("Green")]
	Green = 3
}

For the examples, I have created a small console app with the following Main() method:

static void Main(string[] args)
{
	//Setup enum value
	var enumVal = MyEnum.Blue;

	//TODO: Get enum description

	//TODO: Get enum integer

	//TODO: Parse enum from string
}

Get Enum Description

Next, let’s demonstrate how to retrieve the string description associated with the enum value using our new enum extension method.

//Get enum description
Console.WriteLine("Test getting description text from enum");
string enumText = Enum.GetDescription(MyEnum.Blue);
Console.WriteLine(enumText);
Console.ReadKey();

By running the console application, you should get “Blue”.

Get Enum Integer

Next, let’s simply get the integer value associated with the blue enum.

//Get enum integer
Console.WriteLine("Test getting integer from enum");
int enumInt = (int)enumVal;
Console.WriteLine(enumInt);
Console.ReadKey();

After running the console for getting enum integer, you should get “2”.

Parse Enum from String

Finally, let’s get an enum value from a matching string representation. This is actually a parse of string to enum. As a sidenote, if anyone knows of a cleaner way to do this please leave a comment. I’ve never been happy with how wordy and casting this is.

//Parse enum from string
string enumString = "Red";
var enumFromString = (MyEnum)Enum.Parse(typeof(MyEnum), enumString);
Console.WriteLine(enumFromString);
Console.ReadKey();