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
- enum <Enum_name> {
- enumeration list
- };
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.
- public enum Fruits
- {
- Apple=1,
- Banana=2,
- Pomegranates=3
- }
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:
- public enum BookingStatus
- {
- [Display(Name = "User is active.")]
- [Description("This User is active now available.")]
- Active=1,
-
- [Display(Name = "User is cancelled.")]
- [Description("This User is cancelled now, please contact the admin.")]
- Cancelled =2,
-
- [Display(Name = "User is pending.")]
- [Description("This User is pending now, please contact the admin.")]
- Pending =3
- }
Step 2
Now, a user simply wants to get the Active enum, the user can get it like this:
Getting Simply Enum Name
- var value=BookingStatus.Active;
Result
The result inside the variable value is 'Active', as the Enum list contains.
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,
- var value=(short)BookingStatus.Active;
Result
The result inside the variable value is '1' as the Enum list contains.
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
- short value= (short)BookingStatus.Active;
- 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
- public static class Extensions
- {
-
- public static string GetEnumDisplayName(this Enum value)
- {
- FieldInfo fi = value.GetType().GetField(value.ToString());
-
- DisplayAttribute[] attributes = (DisplayAttribute[])fi.GetCustomAttributes(typeof(DisplayAttribute), false);
-
- if (attributes != null && attributes.Length > 0)
- return attributes[0].Name;
- else
- return value.ToString();
- }
Result
The result inside the variable Status is 'User is active.' as the Enum list contains.
Don't forget to use the following namespace on top of the class
- 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
- var value=(short)BookingStatus.Active;
- var description = Extensions.GetDescription((BookingStatus)value);
The GetDescription() Method is here:
- public static string GetDescription(Enum value)
- {
- var enumMember = value.GetType().GetMember(value.ToString()).FirstOrDefault();
- var descriptionAttribute =
- enumMember == null
- ? default(DescriptionAttribute)
- : enumMember.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute;
- return
- descriptionAttribute == null
- ? value.ToString()
- : descriptionAttribute.Description;
- }
Result
The result inside the description is 'This User is active now available.' as the Enum list contains:
- This User is active now available.
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.