c# - Can my enums have friendly names?

ID : 20197

viewed : 10

Tags : c#enumsc#

Top 5 Answer for c# - Can my enums have friendly names?

vote vote

90

You could use the Description attribute, as Yuriy suggested. The following extension method makes it easy to get the description for a given value of the enum:

public static string GetDescription(this Enum value) {     Type type = value.GetType();     string name = Enum.GetName(type, value);     if (name != null)     {         FieldInfo field = type.GetField(name);         if (field != null)         {             DescriptionAttribute attr =                     Attribute.GetCustomAttribute(field,                       typeof(DescriptionAttribute)) as DescriptionAttribute;             if (attr != null)             {                 return attr.Description;             }         }     }     return null; } 

You can use it like this:

public enum MyEnum {     [Description("Description for Foo")]     Foo,     [Description("Description for Bar")]     Bar }  MyEnum x = MyEnum.Foo; string description = x.GetDescription(); 
vote vote

84

Enum value names must follow the same naming rules as all identifiers in C#, therefore only first name is correct.

vote vote

73

If you have the following enum:

public enum MyEnum {     First,     Second,     Third } 

You can declare Extension Methods for MyEnum (like you can for any other type). I just whipped this up:

namespace Extension {     public static class ExtensionMethods {         public static string EnumValue(this MyEnum e) {             switch (e) {                 case MyEnum.First:                     return "First Friendly Value";                 case MyEnum.Second:                     return "Second Friendly Value";                 case MyEnum.Third:                     return "Third Friendly Value";             }             return "Horrible Failure!!";         }     } } 

With this Extension Method, the following is now legal:

Console.WriteLine(MyEnum.First.EnumValue()); 

Hope this helps!!

vote vote

69

No, but you can use the DescriptionAttribute to accomplish what you're looking for.

vote vote

57

You can use the Description attribute to get that friendly name. You can use the code below:

public static string ToStringEnums(Enum en) {     Type type = en.GetType();      MemberInfo[] memInfo = type.GetMember(en.ToString());     if (memInfo != null && memInfo.Length > 0)     {         object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);         if (attrs != null && attrs.Length > 0)             return ((DescriptionAttribute)attrs[0]).Description;     }     return en.ToString(); } 

An example of when you would want to use this method: When your enum value is EncryptionProviderType and you want enumVar.Tostring() to return "Encryption Provider Type".

Prerequisite: All enum members should be applied with the attribute [Description("String to be returned by Tostring()")].

Example enum:

enum ExampleEnum {     [Description("One is one")]     ValueOne = 1,     [Description("Two is two")]     ValueTow = 2 } 

And in your class, you would use it like this:

ExampleEnum enumVar = ExampleEnum.ValueOne; Console.WriteLine(ToStringEnums(enumVar)); 

Top 3 video Explaining c# - Can my enums have friendly names?

Related QUESTION?