Yes, I'm aware you can easily get the string value of an Enum by using .ToString(). And that's great in many circumstances. I recently came across one where it was important to us to be able to translate an Enum to a custom string that included spaces (which you can't do with an Enum's value). I should have bookmarked the SO answer where I got this code from, but I didn't (sorry, Original Author, whoever you are!).
This extension method takes advantage of the built-in DescriptionAttribute. We decorate our Enum values with the DescriptionAttribute and provide whatever we want, like this:
1: public enum ContactTypes
2: {
3: [Description("Phone Call")]
4: Phone,
5: Email,
6: Chat
7: }
Once we've decorated the Enum values, we'll need the extension method to get the description we just provided.
1: public static string ToDescription(this Enum value)
2: {
3: var da = (DescriptionAttribute[]) value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
4:
5: return da.Length > 0 ? da[0].Description : value.ToString();
6: }
Now we just need to use the extension method on our Enum to get the description we specified. Let's say we have a class that looks like this:
1: public class Interaction
2: {
3: public string UserName { get; set; }
4:
5: public ContactTypes ContactType { get; set; }
6: }
If we wanted to get "Phone Call", "Email", and "Chat" returned (depending on which one was assigned in the class), we'd use the ToDescription extension method like this:
1: var contact = new Interaction { ContactType = ContactTypes.Phone, UserName = "engineer-andrew" };
2: var contactType = contact.ContactType.ToDescription();
Since we didn't use the DescriptionAttribute on Chat and Email, they'll default to just use .ToString() so they'd return "Chat" and "Email", respectively.
That's all there is to it. I know it's kinda simple, but I've used it a couple of times and my rule is to blog about those things. In the next post I'll show how to go the other way (take a string and find its matching Enum value).