C# Getting Enum values

后端 未结 11 1177
深忆病人
深忆病人 2020-12-12 15:25

I have a enum containing the following (for example):

  • UnitedKingdom,
  • UnitedStates,
  • France,
  • Portugal

In my code I use

11条回答
  •  既然无缘
    2020-12-12 16:01

    Just use the DescriptionAttribute

    No need to create a dictionary if you only need to get a String representation for your enum values. See this example

    [EDIT] Oh ... forgot to mention that it is more reusable than dictionaries, since you only need one common util class to help with getting the description and then all you need to do is add the DescriptionAttribute next time you add an enum value or you create a new enum with the same requirements. In the dictionary/switch solution, it is harder to maintain and it gets messy once you have many enum types.

提交回复
热议问题