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
- }
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;
The result inside the variable value is 'Active', as the Enum list contains.
- 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,
- var value=(short)BookingStatus.Active;
Result
The result inside the variable value is '1' as the Enum list contains.
- 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

Reza NabilooPosted Apr 14, 2021, 8:42 AM
My enums in another class library...
Reza NabilooPosted Apr 14, 2021, 8:41 AM
Hi. where i should put this 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(); }
Reza NabilooPosted Apr 14, 2021, 8:41 AM
Hi. where we should write this class???
Taniksha lPosted Oct 23, 2020, 3:28 AM
This is really helpful while getting values from enum