Enum member names are limited to identifiers, so SiteType.Technology cannot read “This is a technology site.” when you show it to a user. A common fix is to put a [Description] attribute on each member and read it back with reflection when you need the text.
The extension method
using System.ComponentModel;
using System.Reflection;
Console.WriteLine(SiteType.Technology.GetDescription());
// This is a technology site.
Console.WriteLine(SiteType.Sports.GetDescription());
// Sports
public enum SiteType
{
[Description("This is a technology site.")]
Technology,
[Description("This is a news site.")]
News,
Sports
}
public static class EnumExtensions
{
public static string GetDescription(this Enum value)
{
FieldInfo? field = value.GetType().GetField(value.ToString());
return field?.GetCustomAttribute<DescriptionAttribute>()?.Description
?? value.ToString();
}
}
Each enum member is compiled as a public static field of the enum type, and the attribute is attached to that field. So the method asks for the field whose name matches the value, then reads its DescriptionAttribute. Members without the attribute, like Sports, fall back to their name.
GetCustomAttribute<T>() is an extension method in System.Reflection.CustomAttributeExtensions. It returns null when the attribute is missing, which is simpler than the older approach of calling GetCustomAttributes(typeof(DescriptionAttribute), false) and checking the array length.
Values without a matching field
value.ToString() does not always return a member name, and then GetField returns null. That happens in two cases:
- A value that is not defined in the enum, such as
(SiteType)42. ItsToString()is"42". - A combination of
[Flags]values. ItsToString()is something like"Read, Write".
The null-conditional ?. in the method handles both, returning the ToString() result instead of throwing a NullReferenceException. If you need descriptions for combined flags, split the value into its individual flags first and join their descriptions.
Caching the descriptions
Reflection is not free. Calling GetDescription in a tight loop, or for every row of a large grid, repeats the same lookup over and over. Since an enum’s attributes never change at run time, compute them once per type:
public static class EnumDescriptions<T> where T : struct, Enum
{
private static readonly Dictionary<T, string> Cache =
Enum.GetValues<T>()
.Distinct()
.ToDictionary(v => v, v => v.GetDescription());
public static string Get(T value) =>
Cache.TryGetValue(value, out string? description)
? description
: value.ToString();
}
Usage: EnumDescriptions<SiteType>.Get(SiteType.News). The static field is initialised once per enum type, the first time it is used, and the runtime makes that initialisation thread-safe. Distinct() is there because an enum can have two members with the same value, and ToDictionary would otherwise throw on the duplicate key.
Alternatives worth knowing
[Display(Name = "...")] from System.ComponentModel.DataAnnotations is what ASP.NET Core’s tag helpers and many UI libraries look for, and it supports localization through ResourceType. If the text is shown in a web UI, it is often a better fit than [Description].
JSON names. If you want an enum serialized with a custom string, an attribute read by your own code will not help. With System.Text.Json, use JsonStringEnumConverter, and on .NET 9 and later put [JsonStringEnumMemberName("...")] on individual members.
A plain switch. For a small enum that is displayed in one place, a switch expression that maps each value to its text is faster, needs no reflection, and works with trimming and Native AOT without extra care.
This solution creates a pair of extension methods on Enum. The first allows you to use reflection to retrieve any attribute associated with your value. The second specifically calls retrieves the