32

How can I achieve the following using enums in .NET? I would like to have descriptions for each value that include spaces.

public enum PersonGender
    {
        Unknown = 0,
        Male = 1,
        Female = 2,
        Intersex = 3,
        Indeterminate = 3,
        Non Stated = 9,
        Inadequately Described = 9
    }

I would like to be able to choose whether to use either the description or integer each time I use a value of this type.

CJ7
  • 22,579
  • 65
  • 193
  • 321
  • Related answer on another post - [Can my enums have friendly names?](https://stackoverflow.com/a/1415460/465053) – RBT May 03 '21 at 11:13

3 Answers3

52

No that's not possible, but you can attach attributes to enum members. The EnumMemberAttribute is designed exactly for the purpose you described.

public enum PersonGender
{
    Unknown = 0,
    Male = 1,
    Female = 2,
    Intersex = 3,
    Indeterminate = 3,

    [EnumMember(Value = "Not Stated")]
    NonStated = 9,

    [EnumMember(Value = "Inadequately Described")]
    InadequatelyDescribed = 9
}

For more information on how to use the EnumMemberAttribute to convert strings to enum values, see this thread.

Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Can a `DescriptionAttribute` be used? – CJ7 Mar 17 '13 at 07:51
  • 1
    Yes it can. In fact, any attribute class that has `AttributeTargets.Field` or `AttributeTargets.All` can be used. You can also write your own. Here are examples using [`DesciptionAttribute`](http://tinyurl.com/clqr3ht) and a [custom attribute](http://tinyurl.com/cyd2b6q). – p.s.w.g Mar 17 '13 at 15:15
  • +1 Very clear. I have a question though. I get `The type or namespace name 'EnumMember' could not be found ...` How does it get included in one's project? After a Google search, I found 3 results and none of them addressed the problem. – demongolem Sep 25 '13 at 15:31
  • 1
    Oh sorry, I see, first I have to include `System.Runtime.Serialization` as a resource in my project and then put a using statement in the file – demongolem Sep 25 '13 at 15:37
  • @p.s.w.g I guess my project doesn't have the assembly or reference added. What's the reference lib for this? – bonCodigo Oct 31 '14 at 05:53
  • 1
    @bonCodigo For any class reference you find on MSDN, there's a namespace and assembly listed just above the syntax. The assembly is what you need to add to your project as a reference, and the namespace is what you need to add at the top of your file as a `using` directive. In this case, both are `System.Runtime.Serialization` – p.s.w.g Oct 31 '14 at 12:06
  • @p.s.w.g I found it and added, however still at the same slogged phase. It is just not recognizing the darn `enummember...` or `description` – bonCodigo Nov 01 '14 at 02:23
  • @bonCodigo Perhaps you need to clean your solution and rebuild. See [Building and Cleaning Projects and Solutions in Visual Studio](http://msdn.microsoft.com/en-us/library/5tdasz7h.aspx) – p.s.w.g Nov 01 '14 at 12:40
  • add reference System.Runtime.Serialization to project – Samra Jul 14 '17 at 01:59
5

This is easy. Create an extension method for your string that returns a formatted string based on your coding convention. You can use it in lots of places, not just here. This one works for camelCase and TitleCase.

    public static String ToLabelFormat(this String s)
    {
        var newStr = Regex.Replace(s, "(?<=[A-Z])(?=[A-Z][a-z])", " ");
        newStr = Regex.Replace(newStr, "(?<=[^A-Z])(?=[A-Z])", " ");
        newStr = Regex.Replace(newStr, "(?<=[A-Za-z])(?=[^A-Za-z])", " ");

        return newStr;
    }
theMayer
  • 15,456
  • 7
  • 58
  • 90
1
var assembly = Assembly.LoadFrom("ResourcesLib.DLL");            
var resourceManager =
new ResourceManager("ResourcesLib.EnumDescriptions", assembly);                        

var lst = Enum.GetValues(typeof(PersonGender)).Cast<PersonGender>().ToList();
foreach (var gender in lst)
{
  Console.WriteLine(gender); // Name
  Console.WriteLine((int)gender); //Int Value
  Console.WriteLine(resourceManager.GetString(gender.ToString()));//localized Resorce
}          

So spaces may reside in localized resource ...

Mohsen Heydari
  • 7,256
  • 4
  • 31
  • 46