Click here to Skip to main content
Click here to Skip to main content

Mapping Text to Enum entries

, 6 Jun 2003
Rate this:
Please Sign up or sign in to vote.
Attaching a description to each entry in an enum.

Sample Image - EnumWithDescription.png


While porting old C code to C#, I stumbled across lots of huge arrays which mapped constants to strings. While looking for the C# way of doing this, I discovered custom attributes and reflection.

How is it done?

Append a Description attribute to each enum entry:

private enum MyColors
   [Description("yuk!")]       LightGreen    = 0x012020,
   [Description("nice :-)")]   VeryDeepPink  = 0x123456,
   [Description("so what")]    InvisibleGray = 0x456730,
   [Description("no comment")] DeepestRed    = 0xfafafa,
   [Description("I give up")]  PitchBlack    = 0xffffff,

To access the description from code, the following function is called:

public static string GetDescription(Enum value)
   FieldInfo fi= value.GetType().GetField(value.ToString()); 
   DescriptionAttribute[] attributes = 
         typeof(DescriptionAttribute), false);
   return (attributes.Length>0)?attributes[0].Description:value.ToString();

Without the following using declaration, the code will not compile.

using System.ComponentModel;        
using System.Reflection;


Instead of having constants and huge arrays all over the code, the above solution keeps declarations and descriptions nicely together.


I've only found out by accident that the framework already provides a DescriptionAttribute class.

I built the first version with my own custom attribute and got conflicts when I started using the ComponentModel namespace.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Reto Ravasio

Switzerland Switzerland
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pinmemberlendres3-Apr-12 8:14 
GeneralMy vote of 5 PinmemberDorofejev20-Dec-11 0:44 
QuestionHow to use with dataset / tableadapter PinmemberSaelen Kenny8-Nov-09 1:01 
QuestionQuery regarding description Tag in Enum PinmemberNITINKADAM24-Nov-08 6:14 
GeneralAwesome - thanks! Pinmember93522-Jun-07 6:48 
GeneralDifferent versions for speed improvements Pinmemberclementgatin12-Mar-07 12:37 
GeneralThanks again PinprotectorMarc Clifton15-Oct-06 4:39 
GeneralSolution for C / C++ PinmemberJimmyO5-May-05 10:56 
GeneralRe: Solution for C / C++ PinsitebuilderUwe Keim10-Apr-06 20:53 
Questionwhy??? Pinmemberkoo918-Jan-05 11:53 
AnswerRe: why??? PinmemberReto Ravasio19-Jan-05 9:36 
QuestionWhat about Flags? PinmemberSpliffy7322-Mar-04 5:22 
AnswerRe: What about Flags? PinmemberJSpens14-Aug-07 8:50 
AnswerRe: What about Flags? Pinmemberfrinkfree11-Nov-08 13:55 
GeneralAutoDoc PinsussDave Miller4-Feb-04 18:50 
GeneralI liked this PinmemberPalladino26-Aug-03 3:39 
I liked this. Great idea!! Here it is going an idea on the message posted by Uwe Keim. The attribute could contain a property that would be destined to the name of resource (e.g. "AnyLib.Resources.Common.Yes"). In moment of solving string (GetDescription method) the mechanism could verify this property, if search be filled out the resource otherwise it uses the default description (hardcoded).
Marcelo Palladino
QuestionWhat About using the System Enum Pinmembervbnetuk25-Jun-03 23:10 
AnswerRe: What About using the System Enum PinmemberReto Ravasio4-Jul-03 2:04 
GeneralRe: What About using the System Enum Pinmembervbnetuk6-Jul-03 22:05 
GeneralRe: What About using the System Enum PinmemberReto Ravasio7-Jul-03 5:47 
GeneralRe: What About using the System Enum Pinmemberdevaulr6-Nov-03 3:40 
GeneralRe: What About using the System Enum Pinmemberdatavalue19-Oct-05 4:06 
AnswerRe: What About using the System Enum PinmemberConradC1-Feb-05 9:22 
GeneralMultilingual PinsitebuilderUwe Keim7-Jun-03 22:36 
GeneralRe: Multilingual PineditorHeath Stewart27-Oct-03 6:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 7 Jun 2003
Article Copyright 2003 by Reto Ravasio
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid