Fetching Values From Enum In C#

Introduction

 
In this article, I will be explaining and showing to you by code sample how you can define and display friendly names for enumeration elements using the Description annotation. With this, we will see how to get an Enum value and Name.
 

Overview of Enum

  • An Enumeration (or enum) is a data type that includes a set of named values called elements or members.
  • An Enum is a type declared using the enum keyword.
Syntex
  1. enum <Enum_name> {  
  2.    enumeration list  
  3. };  
Where the enum is the keyword, and <Enum_name> specifies the Name of an Enum, under this list of enum name would be declared
 
Eg. 
  1. public enum Fruits  
  2. {  
  3.    Apple=1,  
  4.    Banana=2,  
  5.    Pomegranates=3  
  6. }  
So now, let's start with an example in C# to understand it in detail.
 
Step 1
 
Create an Enum.cs class and make an enum in this class: 
  1. public enum BookingStatus  
  2.    {  
  3.        [Display(Name = "User is active.")]  
  4.        [Description("This User is active now available.")]  
  5.        Active=1,  
  6.   
  7.        [Display(Name = "User is cancelled.")]  
  8.        [Description("This User is cancelled now, please contact the admin.")]  
  9.        Cancelled =2,  
  10.   
  11.        [Display(Name = "User is pending.")]  
  12.        [Description("This User is pending now, please contact the admin.")]  
  13.        Pending =3  
  14.    }  
Step 2
 
Now, a user simply wants to get the Active enum, the user can get it like this:
 
Getting Simply Enum Name
  1. var value=BookingStatus.Active;  
Result 
 
The result inside the variable value is 'Active', as the Enum list contains.
  1. Active  
Step 3
 
Now, the user wants to get the Active enum value (that is 1), the user can simply get it like this -
 
Getting Value From Enum,
  1. var value=(short)BookingStatus.Active;  
Result
 
The result inside the variable value is '1' as the Enum list contains.
  1. 1  
Step 4
 
Now, the user wants to get the Active enum Display Name (that is "User is active."), the user can get it like this:
 
Getting Enum Display Name from Enum Value
  1. short value= (short)BookingStatus.Active;  
  2. var   Status = Extensions.GetEnumDisplayName((BookingStatus)value);  
Here, Extensions is a static class in which I have created the static GetEnumDisplayName() function for getting an enum display name from its value
 
Extension.cs class 
  1. public static class Extensions  
  2.    {  
  3.   
  4. public static string GetEnumDisplayName(this Enum value)  
  5.        {  
  6.            FieldInfo fi = value.GetType().GetField(value.ToString());  
  7.   
  8.            DisplayAttribute[] attributes = (DisplayAttribute[])fi.GetCustomAttributes(typeof(DisplayAttribute), false);  
  9.   
  10.            if (attributes != null && attributes.Length > 0)  
  11.                return attributes[0].Name;  
  12.            else  
  13.                return value.ToString();  
  14.        }  
Result
 
The result inside the variable Status is 'User is active.' as the Enum list contains.
  1. User is active.  
Note
Don't forget to use the following namespace on top of the class 
  1. using System.ComponentModel.DataAnnotations;  
Step 5
 
Now, the user wants to get the Active enum Description (that is "This User is active now available."), the user can get it like this:
 
Getting Enum Description from Enum Value using Description attribute
  1. var value=(short)BookingStatus.Active;  
  2. var description = Extensions.GetDescription((BookingStatus)value);  
The GetDescription() Method is here:
  1. public static string GetDescription(Enum value)  
  2.       {  
  3.           var enumMember = value.GetType().GetMember(value.ToString()).FirstOrDefault();  
  4.           var descriptionAttribute =  
  5.               enumMember == null  
  6.                   ? default(DescriptionAttribute)  
  7.                   : enumMember.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute;  
  8.           return  
  9.               descriptionAttribute == null  
  10.                   ? value.ToString()  
  11.                   : descriptionAttribute.Description;  
  12.       }  
Result
 
The result inside the description is 'This User is active now available.' as the Enum list contains:
  1. This User is active now available.  
Fetching Values From Enum in c# 
 

Conclusion

 
So, in the above article, 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.