Click here to Skip to main content
12,289,383 members (72,450 online)
Click here to Skip to main content
Add your own
alternative version


89 bookmarked

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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
lendres3-Apr-12 7:14
memberlendres3-Apr-12 7:14 
GeneralMy vote of 5 Pin
Dorofejev19-Dec-11 23:44
memberDorofejev19-Dec-11 23:44 
QuestionHow to use with dataset / tableadapter Pin
Saelen Kenny8-Nov-09 0:01
memberSaelen Kenny8-Nov-09 0:01 
QuestionQuery regarding description Tag in Enum Pin
NITINKADAM24-Nov-08 5:14
memberNITINKADAM24-Nov-08 5:14 
GeneralAwesome - thanks! Pin
93522-Jun-07 5:48
member93522-Jun-07 5:48 
GeneralDifferent versions for speed improvements Pin
clementgatin12-Mar-07 11:37
memberclementgatin12-Mar-07 11:37 
GeneralThanks again Pin
Marc Clifton15-Oct-06 3:39
protectorMarc Clifton15-Oct-06 3:39 
GeneralSolution for C / C++ Pin
JimmyO5-May-05 9:56
memberJimmyO5-May-05 9:56 
GeneralRe: Solution for C / C++ Pin
Uwe Keim10-Apr-06 19:53
sitebuilderUwe Keim10-Apr-06 19:53 
Questionwhy??? Pin
koo918-Jan-05 10:53
memberkoo918-Jan-05 10:53 
AnswerRe: why??? Pin
Reto Ravasio19-Jan-05 8:36
memberReto Ravasio19-Jan-05 8:36 
QuestionWhat about Flags? Pin
Spliffy7322-Mar-04 4:22
memberSpliffy7322-Mar-04 4:22 
AnswerRe: What about Flags? Pin
JSpens14-Aug-07 7:50
memberJSpens14-Aug-07 7:50 
AnswerRe: What about Flags? Pin
frinkfree11-Nov-08 12:55
memberfrinkfree11-Nov-08 12:55 
This method attempts to break an enum down into its greatest common values. The return value indicates if the enum was completely covered by the returned enum value list. Perhaps it should be called TryGetEnumValues Wink | ;)
private bool GetEnumValues(Type enumType, object value, out IEnumerable<object> enumValues)
  bool enumCompletelyCovered = false;
  List<object> enumValueCollection = new List<object>();
  // perform a shortcut comparison for the common case
  if(Enum.IsDefined(enumType, value))
    // the specified value completely matches an enumeration entry
    enumCompletelyCovered = true;
    // attempt to decompose the specified value into several enumeration entries
    // compare enumeration values as the largest comparable type (UInt64)
    ulong currentEnumValue = Convert.ToUInt64(value);
    // get an array of all the possible enumeration entries
    Array values = Enum.GetValues(enumType);
    // process the enumeration entries in reverse order to compare the largest values first
    int index = values.Length - 1;
    while(index >= 0)
      // convert currently indexed enumeration entry value to a comparable type
      ulong currentIndexedValue = System.Convert.ToUInt64(values.GetValue(index));
      // special case the None enumeration
      if((index == 0) && (currentIndexedValue == 0L))
      // use a bit-field comparison technique
      if((currentEnumValue & currentIndexedValue) == currentIndexedValue)
        currentEnumValue -= currentIndexedValue;
      // move to the next up enumeration entry
    // special case the None enumeration
    // Add the assumed None enumeration if:
    //  the enumeration value collection is empty;
    //  and the current value is 0;
    //  and the current enumeration entry is the first entry;
    //  and the current enumeration entry matches the current value.
    if((enumValueCollection.Count == 0) && (currentEnumValue == 0L) && (index == 0) && (System.Convert.ToUInt64(values.GetValue(index)) == 0L))
    // reverse the enumeration value collection to go from least to greatest values
    // the enumeration was completely covered if the current value is 0 and there is at lease one entry in the enumeration value collection
    enumCompletelyCovered = (currentEnumValue == 0L) && (enumValueCollection.Count > 0);
  enumValues = enumValueCollection;
  return enumCompletelyCovered;
Hope this helps, -Parx
GeneralAutoDoc Pin
Dave Miller4-Feb-04 17:50
sussDave Miller4-Feb-04 17:50 
GeneralI liked this Pin
Palladino26-Aug-03 2:39
memberPalladino26-Aug-03 2:39 
QuestionWhat About using the System Enum Pin
vbnetuk25-Jun-03 22:10
membervbnetuk25-Jun-03 22:10 
AnswerRe: What About using the System Enum Pin
Reto Ravasio4-Jul-03 1:04
memberReto Ravasio4-Jul-03 1:04 
GeneralRe: What About using the System Enum Pin
vbnetuk6-Jul-03 21:05
membervbnetuk6-Jul-03 21:05 
GeneralRe: What About using the System Enum Pin
Reto Ravasio7-Jul-03 4:47
memberReto Ravasio7-Jul-03 4:47 
GeneralRe: What About using the System Enum Pin
devaulr6-Nov-03 2:40
memberdevaulr6-Nov-03 2:40 
GeneralRe: What About using the System Enum Pin
datavalue19-Oct-05 3:06
memberdatavalue19-Oct-05 3:06 
AnswerRe: What About using the System Enum Pin
ConradC1-Feb-05 8:22
memberConradC1-Feb-05 8:22 
GeneralMultilingual Pin
Uwe Keim7-Jun-03 21:36
sitebuilderUwe Keim7-Jun-03 21:36 
GeneralRe: Multilingual Pin
Heath Stewart27-Oct-03 5:07
editorHeath Stewart27-Oct-03 5:07 
GeneralRe: Multilingual Pin
Uwe Keim7-Mar-05 19:28
sitebuilderUwe Keim7-Mar-05 19:28 
GeneralRe: Multilingual Pin
Craig Fisher30-Oct-07 9:18
memberCraig Fisher30-Oct-07 9:18 
GeneralThe descriptions will also show up in the property grid Pin
leppie7-Jun-03 10:07
memberleppie7-Jun-03 10:07 
GeneralRe: The descriptions will also show up in the property grid Pin
Reto Ravasio7-Jun-03 23:34
memberReto Ravasio7-Jun-03 23:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.160518.1 | Last Updated 7 Jun 2003
Article Copyright 2003 by Reto Ravasio
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid