404 Mapping Enums to Strings and Strings to Enums in .NET | Kevin William Pang

Mapping Enums to Strings and Strings to Enums in .NET

I recently stumbled across a blog post demonstrating how to convert enums to strings using the Description attribute.  I thought it might be useful to expand on that example by providing a function that reverses the process (e.g. takes in the resulting string and converts it back to the enum).  This would allow you to do things like store off the description into a database and load it back into an enum or populate a dropdown list with the descriptions and save the user's selection back into your domain as an enum.

Rather than diving straight into code, let's just take a quick look at what the finished product lets you do.  Say you have the following enumeration:

The code from the original blog post will allow you to retrieve any of the Description attributes by calling the ToDescription extension method:

And with the code I wrote, you will be able to perform the opposite conversion by calling the ToEnum extension method:

Ok, now let's look at how it all works:

Here is the slightly modified version of the extension method from the original blog post.  It's pretty straightforward.  It's an extension method on the Enum class that uses reflection to grab the Description attribute and output it.  If no Description attribute is found, it returns the result of the ToString() function: 

And here is my extension method that reverses the process.  This extension method on the string class iterates over all enum values for the given enumeration, trying to find the one that has a Description attribute that matches the string that called it.  If the extension method can't find this enum, it returns the default enum passed in the parameters.

I'm sure there are probably cleaner ways of doing this, but for mypurposes this works great and is easy to implement and manage.  I'd be interested in hearing how others are handling this scenario in their own apps and whether this implementation would be useful at all.  Let me know in the comments!


Kevin Pang

Kevin is a software engineer at Google whose programming interests revolve around web development, software architecture, and design. When he's not writing software, he enjoys watching Jeopardy!, playing Magic the Gathering, and wandering around Disneyland.


10 thoughts on “Mapping Enums to Strings and Strings to Enums in .NET

  1. Hi,
    nict post, but one question: your description and enum-values are equal, so why don’t you just use the built-in functions "Enum.ToString()" and "Enum.Parse()"? does this method give you any benefits?
    greets, chris.

  2. @Christian

    Yes, you are correct. For this simple example using those two functions will suffice. But if you need to have, say, spaces in your description (which aren’t supported in enums), or just a description that deviates from the enum value at all, then you would need to use another approach. That is where this implementation comes in handy.

  3. @Ryan

    I’m glad you found it helpful.

    I wish I thought of doing this before too. Now I’m faced with the question of whether or not to refactor all my old mapping functions to use this. :-P

  4. Very Swanky- I’m sticking this in my toolbox class, will definitely come in handy in cleaning up some UI code of mine (specifically radio buttons which choose a value passed as an enum- Was if/elsing the crap out of it:D )

  5. What if we have to bind the enum with database? Would it be possible by extending this extension method?

  6. Nice post. This proved really useful for mapping enums to to types on a legacy database. Cheers!!

Comments are closed.