Mark S. Rasmussen improve.dk
Mar 13
2008

Switches are rarely nice in an architectural aspect, but they are often required none the less. One of the ways we can reduce the risk of errors as well as increase readability is to use enumeration values instead of constants. Unfortunately this only works for numeric types, we cannot create a string enumeration. Here’s a workaround. This is a typical console application, taking in an input value (stored in the input variable) and switching on the content:

using System;

namespace StringEnumeration
{
	class Program
	{
		static void Main(string[] args)
		{
			string input = "Hello";

			switch (input)
			{
				case "Hello":
					Console.WriteLine("Hello world!");
					break;
				case "Goodbye":
					Console.WriteLine("Goodbye world!");
					break;
				default:
					Console.WriteLine("Does not compute!");
					break;
			}
		}
	}
}

The first step is to define the enumeration of values we need to have in our switch statement:

enum Input
{
	Hello,
	Goodbye
}

We cannot convert from strings to the Input enumeration type directly, so we’ll have to use a magic function like this:

class EnumHelper
{
	public static T Parse<T>(string input)
	{
		return (T)Enum.Parse(typeof(T), input, true);
	}
}

Using the above function, we can refactor our initial code like so:

string input = "Hello";

switch (EnumHelper.Parse<Input>(input))
{
	case Input.Hello:
		Console.WriteLine("Hello world!");
		break;
	case Input.Goodbye:
		Console.WriteLine("Goodbye world!");
		break;
	default:
		Console.WriteLine("Does not compute!");
		break;
}

Take notice that I’m passing in true as the third parameter of the Enum.Parse method, this means the type conversion will not be case sensitive, you can change this parameter as needed, or maybe refactor it into a parameter of the function. If the conversion fails - if a matching enumeration does not exist - an ArgumentException is thrown.

Mark S. Rasmussen
I'm the CTO at iPaper where I cuddle with databases, mold code and maintain the overall technical & team responsibility. I'm an avid speaker at user groups & conferences. I love life, motorcycles, photography and all things technical. Say hi on Twitter, write me an email or look me up on LinkedIn.