In this blog, I’ll show you a basic extension method that you can use to get the display name of your enum.
Enum Overview
In our sample enum file, we created TransactionStatus
where it has enum values and ids. We also used the using System.ComponentModel.DataAnnotations
so we can use the attribute [Display(Name = "")]
using System.ComponentModel.DataAnnotations;
namespace ConsoleApp {
public enum TransactionStatus
{
[Display(Name = "Pending")]
Pending = 0,
[Display(Name = "In Progress")]
InProgress = 1,
[Display(Name = "Completed")]
Completed = 2,
[Display(Name = "Cancelled")]
Cancelled = 3,
[Display(Name = "For Approval")]
ForApproval = 4
}
}
Extension method
After we create our enum, we can now proceed in creating our extension method. To quick review about extension method, basically, extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. You can also create different extension methods for string, date and other types depending on your requirements. Some of the advantages in using extension methods, it’s simpler and makes your code much more cleaner. Read more from Microsoft Doc
So let’s create our extension method.
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
namespace ConsoleApp
{
public static class EnumExtensions
{
public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
?.GetName();
}
}
}
Sample Usage
Let’s try our extension method.
var status = TransactionStatus.ForApproval;
status.GetDisplayName();
Then it will display For Approval
.
If you have some questions or comments, please drop it below 👇 :)