Last Updated on September 17, 2017 by Aram
Also I will be explaining a great way to blend the extension methods feature to write an easy-to-use extension method to get a generic enum’s description property’s value and return it to the caller process.
ColorsEnum
enumeration type defined in C#:
1 2 3 4 5 6 7 |
public enum ColorsEnum { BlanchedAlmond = 1, DarkSeaGreen = 2, DeepSkyBlue = 3, RosyBrown = 4 } |
To print the enumeration elements names, we will iterate over the names of the Enumeration type, append each name to a string builder and then display it on a message box, see the below code snippet:
1 2 3 4 5 6 7 8 |
var stringBuilder = new StringBuilder(); foreach (string colorEnum in Enum.GetNames(typeof(ColorsEnum))) { stringBuilder.AppendLine(colorEnum); } MessageBox.Show(this, stringBuilder.ToString(), "Enumeration None Friendly Names"); |
The result of executing the above code will be displaying the color names, as they are defined in the enumeration type (in their non-friendly format).
Below is the message box that will be shown once we run the above code:
The above code resulted in printing out the names, as defined in the ColorsEnum
enumeration data type.
As you can note, the names are non-friendly names; that’s it, no spaces between names.
Sometimes we need to print out a given enumeration element name in a user-friendly format, in this case we will need to add an attribute decoration on each of the enumeration names. This attribute comes from the namespace System.ComponentModel.
The below code shows the updated version of the ColorsEnum
, having the new Description attribute decoration applied to the enum
names:
1 2 3 4 5 6 7 8 9 10 11 |
public enum FriendlyColorsEnum { [Description("Blanched Almond Color")] BlanchedAlmond = 1, [Description("Dark Sea Green Color")] DarkSeaGreen = 2, [Description("Deep Sky Blue Color")] DeepSkyBlue = 3, [Description("Rosy Brown Color")] RosyBrown = 4 } |
The Description attribute decoration is used to define a descriptive name for a give property or event. It can be used with either enum elements or with any user defined data type property or event.
The description decoration is defined under the System.ComponentModel
Namespace, which should be imported directly in the code, or included within the project’s default references.
Even though we defined friendly descriptions for each of our enumeration elements, using the normal ToString()
method will keep printing non-friendly names.
So, printing the descriptive friendly names that we defined requires an extension method (Please refer to my blog post Extension Methods in .Net to learn more about writing extension methods).
This extension method will use reflection to get an array of the enum’s members, and then from the array’s first element’s member info it will check and return if the member has a Description attribute defined, if that attribute exists, then it will return the Description value, this will be contained within the GenericEnum
variable’s ToString()
method.
The code below is an extension method that can be used by any enumeration data type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static string GetDescription(this Enum GenericEnum) { Type genericEnumType = GenericEnum.GetType(); MemberInfo[] memberInfo = genericEnumType.GetMember(GenericEnum.ToString()); if ((memberInfo != null && memberInfo.Length > 0)) { var _Attribs = memberInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); if ((_Attribs != null && _Attribs.Count() > 0)) { return ((System.ComponentModel.DescriptionAttribute)_Attribs.ElementAt(0)).Description; } } return GenericEnum.ToString(); } |
Now that we wrote our extension method, we are ready to use this method with our ColorsEnum
datatype.
1 2 3 4 5 |
StringBuilder stringBuilder = new StringBuilder();foreach (ColorsEnum colorEnum in Enum.GetValues(typeof(ColorsEnum))) { stringBuilder.AppendLine(colorEnum.GetDescription()); } MessageBox.Show(this, stringBuilder.ToString(), "Enumeration Friendly Names"); |
As you have noticed, we are using the GetDescription
method as it is part of the ColorsEnum
(This is the power of the extension methods).
The execution result of the above code is illustrated within the below figure:

Conclusion
ToString()
method, it will be displayed as it has been defined. This will give non-friendly look on the UI if the enum’s element name is a compound name.Bonus
My spectacular reader! I am dedicating you a breathtakingly beautiful and relaxing classical music: Mozart – Piano Sonata No. 11 in A major, K. 331 – I. Andante grazioso
Happy coding and listening!