Posted by Anuraj on Thursday, March 19, 2015 Reading time :1 minute
Recently one of my colleague asked me question, he want to create a enum with string values. But C# doesn’t support string based enums, so here is the code snippet which will help you to associate string values to enums. This code is not using any custom attribute, instead it is using DescriptionAttribute class. Here is the enum, with the associated string values.
And here is the extension method, which returns the string value associated to the enum
You can get the description from enum like this.
Happy Programming :)
Update : Based on the comment from NDVictory, source code modified to return the exact value, earlier it was always returning the first value. Thank you NDVictory
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.