DotNET Schools

Blogs & Tutorials for Microsoft.NET and Leading web Technologies

Getting More than 2 Values for an Enum Item

As we know that we can get Enum and it value from an Enum type e.g. I want to create an enum for sorting of article like below:

  1. enum ArticleSortingOptions  
  2. {  
  3.    MostPopularArticle =1,  
  4.    LatestArticles =2,  
  5.    FeaturedArtilces =3,  
  6.    MostLikedArtilces =4,  
  7.    MostCommentedArtilces=5  
  8. }  

And accessing it like

  1. WriteLine(MostPopularArticle.ToString());  
  2. WriteLine((int)MostPopularArticle);  

Output

MostPopularArticle

1

you may be thinking that I am aceesing enum item without enum name. Yes, it is possible in C# 6 & C# 7 that we can acess enum items without enum name for that we have to write enum name in using statement i.e.

  1. using static MultiValuedEnum.ArticleSortingOptions;  

You will never prefer to display the value like “MostPopularArticle” but to be displayed like “Most Popular Article”. As we know that enum will not allow you to write an item with spaces. So we will require 3 types of values

1. Integer value of enum item.

2.   2. Name of enum Item (string Value of enum).

3.   3. Description of Enum (or some other value as per requirement).

We can achieve this by writing and extension method for enum.

  1. public static class ExtendEnum  
  2. {  
  3.    public static string DisplayString<T>(this T enumVal)  
  4.    {  
  5.       FieldInfo fi = enumVal.GetType().GetField(enumVal.ToString());  
  6.       var attrs = fi?.GetCustomAttributes(typeof(DescriptionAttribute), true);  
  7.       if (attrs != null && attrs.Length > 0)  
  8.       {  
  9.          return ((DescriptionAttribute)attrs[0]).Description;  
  10.       }  
  11.       return "No Description found";  
  12.    }  
  13. }  

And access like:

  1. WriteLine(MostPopularArticle.ToString());  
  2. WriteLine((int)MostPopularArticle);  
  3. WriteLine(MostPopularArticle.DisplayString());  

Complete Code:

  1. using System.ComponentModel;  
  2. using System.Reflection;  
  3. using static MultiValuedEnum.ArticleSortingOptions;  
  4. using static System.Console;  
  5.   
  6. namespace MultiValuedEnum  
  7. {  
  8.     enum ArticleSortingOptions  
  9.     {  
  10.         [Description("Most Popular Article")]  
  11.         MostPopularArticle = 1,          
  12.         LatestArticles = 2,  
  13.         FeaturedArtilces = 3,  
  14.         MostLikedArtilces = 4,  
  15.         MostCommentedArtilces = 5  
  16.     }  
  17.     class Program  
  18.     {  
  19.         static void Main(string[] args)  
  20.         {  
  21.             WriteLine(MostPopularArticle.ToString());  
  22.             WriteLine((int)MostPopularArticle);  
  23.             WriteLine(MostPopularArticle.DisplayString());  
  24.         }  
  25.     }  
  26.   
  27.     public static class ExtendEnum  
  28.     {  
  29.         public static string DisplayString<T>(this T enumVal)  
  30.         {  
  31.             FieldInfo fi = enumVal.GetType().GetField(enumVal.ToString());  
  32.             var attrs = fi?.GetCustomAttributes(typeof(DescriptionAttribute), true);  
  33.             if (attrs != null && attrs.Length > 0)  
  34.             {  
  35.                 return ((DescriptionAttribute)attrs[0]).Description;  
  36.             }  
  37.             return "No Description found";  
  38.         }  
  39.     }  
  40. }  

Output

MostPopularArticle

1

Most Popular Article

Converting Enum to Different Format

  1.  //way 1              
  2.  Enum MyNewEnum = (MyColors)Enum.Parse(typeof(MyColors), "RED");         
  3.   
  4.  //Way 2   
  5.  Enum MyNewEnum2 = (MyColors)Enum.Parse(typeof(MyColors), "1");  
  6.   
  7.  //Way 3  
  8.  Enum MyNewEnum3 = (MyColors)1;  
  9.   
  10.  //Way 4  
  11.  Enum MyNewEnum4 = (MyColors)Enum.ToObject(typeof(MyColors), 1);  
  12.   
  13.  //Way 5  
  14.  Enum MyNewEnum5 = (MyColors)3;  
  15.   
  16.  //Way 6  
  17.  string myEnumString  = ((MyColors)1).ToString("F");  
  18.  string myEnumString2 = ((MyColors)1).ToString("F");  
  19.   
  20.  string myEnumString3 = MyColors.BLUE.ToString("d");  
  21.  string myEnumString4 = MyColors.BLUE.ToString("D");  
  22.                                  
  23.  string myEnumString5 = MyColors.BLUE.ToString("g");  
  24.  string myEnumString6 = MyColors.BLUE.ToString("G");  
  25.                                  
  26.  string myEnumString7 = MyColors.BLUE.ToString("x");  
  27.  string myEnumString8 = MyColors.BLUE.ToString("X");  
  28.   
  29.  //Way 7  
  30.  string[] colorrList  = Enum.GetNames(typeof(MyColors));  
  31.  string myEnumString9 = Enum.GetName(typeof(MyColors), 5);  
  32.   
  33.  //way 8  
  34. string myFormatEnumString1 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "d");  
  35. string myFormatEnumString2 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "D");  
  36. string myFormatEnumString3 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "f");  
  37. string myFormatEnumString4 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "F");  
  38. string myFormatEnumString5 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "g");  
  39. string myFormatEnumString6 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "G");  
  40. string myFormatEnumString7 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "x");  
  41. string myFormatEnumString8 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "X");