C# || How To Set & Get The Description Of An Enum Using C#
The following is a module with functions which demonstrates how to set and get the description of an enum using C#.
This function uses the DescriptionAttribute to get the description of an Enum element. If a description attribute is not specified, the string value of the Enum element is returned.
1. Get Description
The example below demonstrates the use of ‘Utils.Extensions.GetDescription‘ to get the description of enum elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// Get Description using Utils; public enum MimeTypes { [System.ComponentModel.Description("application/pdf")] ApplicationPDF = 1, [System.ComponentModel.Description("application/octet-stream")] ApplicationOctect = 2, [System.ComponentModel.Description("application/zip")] ApplicationZip = 3, [System.ComponentModel.Description("application/msword")] ApplicationMSWordDOC = 4, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.wordprocessingml.document")] ApplicationMSWordDOCX = 5, [System.ComponentModel.Description("application/vnd.ms-excel")] ApplicationMSExcelXLS = 6, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")] ApplicationMSExcelXLSX = 7 } foreach (MimeTypes value in System.Enum.GetValues(typeof(MimeTypes))) { Console.WriteLine($"Numeric Value: {(int)value}, Name: {value.ToString()}, Description: {value.GetDescription()}"); } // expected output: /* Numeric Value: 1, Name: ApplicationPDF, Description: application/pdf Numeric Value: 2, Name: ApplicationOctect, Description: application/octet-stream Numeric Value: 3, Name: ApplicationZip, Description: application/zip Numeric Value: 4, Name: ApplicationMSWordDOC, Description: application/msword Numeric Value: 5, Name: ApplicationMSWordDOCX, Description: application/vnd.openxmlformats-officedocument.wordprocessingml.document Numeric Value: 6, Name: ApplicationMSExcelXLS, Description: application/vnd.ms-excel Numeric Value: 7, Name: ApplicationMSExcelXLSX, Description: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 9, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; using System.Linq; namespace Utils { public static class Extensions { /// <summary> /// Returns the description of an enum decorated with a DescriptionAttribute /// </summary> /// <param name="value">An Enumeration value</param> /// <returns>The description of an enumeration value</returns> public static string GetDescription(this System.Enum value) { var description = value.ToString(); var attribute = GetAttribute<System.ComponentModel.DescriptionAttribute>(value); if (attribute != null) { description = attribute.Description; } return description; } public static T GetAttribute<T>(System.Enum value) where T : System.Attribute { var field = value.GetType().GetField(value.ToString()); var attribute = ((T[])field.GetCustomAttributes(typeof(T), false)).FirstOrDefault(); return attribute; } } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 9, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using Utils; public class Program { public enum MimeTypes { [System.ComponentModel.Description("application/pdf")] ApplicationPDF = 1, [System.ComponentModel.Description("application/octet-stream")] ApplicationOctect = 2, [System.ComponentModel.Description("application/zip")] ApplicationZip = 3, [System.ComponentModel.Description("application/msword")] ApplicationMSWordDOC = 4, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.wordprocessingml.document")] ApplicationMSWordDOCX = 5, [System.ComponentModel.Description("application/vnd.ms-excel")] ApplicationMSExcelXLS = 6, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")] ApplicationMSExcelXLSX = 7, [System.ComponentModel.Description("application/vnd.ms-powerpoint")] ApplicationMSPowerPointPPT = 8, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.presentationml.presentation")] ApplicationMSPowerPointPPTX = 9, [System.ComponentModel.Description("audio/basic")] AudioBasic = 10, [System.ComponentModel.Description("audio/mpeg")] AudioMPEG = 11, [System.ComponentModel.Description("audio/x-wav")] AudioWAV = 12, [System.ComponentModel.Description("image/gif")] ImageGIF = 13, [System.ComponentModel.Description("image/jpeg")] ImageJPEG = 14, [System.ComponentModel.Description("image/png")] ImagePNG = 15, [System.ComponentModel.Description("text/html")] TextHTML = 16, [System.ComponentModel.Description("text/plain")] TextPlain = 17, [System.ComponentModel.Description("video/mpeg")] VideoMPEG = 18, [System.ComponentModel.Description("video/x-msvideo")] VideoAVI = 19 } static void Main(string[] args) { try { foreach (MimeTypes value in System.Enum.GetValues(typeof(MimeTypes))) { Display($"Numeric Value: {(int)value}, Name: {value.ToString()}, Description: {value.GetDescription()}"); } } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply