SoFunction
Updated on 2025-03-01

How to add a description method to enum types in C# [Tips]

background

In our daily development, we often use enum types. An enum type is a unique value type with a set of named constants. In the following example:

enum Color
{ 
  Red,
  Green,
  Blue
}

Declare an enumeration type named Color that has three members: Red, Green, and Blue.

How to declare the enumeration? Enumeration declaration is used to declare new enum types. The enum declaration starts with the keyword enum and then defines the name, accessibility, base type, and member of the enum type. Specific format:

Modifier (new, public, protected, internal, private) enum enum type name: integer type

{ 
  enum-member-declarations,
  enum-member-declaration
}

Sometimes we only need to display the enum value or the corresponding name of the enum value, but in some scenarios, we may need to display the enum value as a different string.

Example: Currently we have the following enumeration Level

 public enum Level
 {
  //Bad
  B = -1,

  //Normal
  N = 0,

  //Good
  G = 1,

  //Very Good 
  VG = 2
 }

This enum has 4 optional values ​​B, N, G, VG. Now we hope to use Bad, Normal, Good, Very Good as the display values ​​of B, N, G, VG.

So what will we do? Usually what we think of most is to write an extension method for Level enum types.

 public static class LevelEnumExtension
 {
  public static string ToDescription(this Level level)
  {
   switch (level)
   {
    case :
     return "Bad";
    case :
     return "Good";
    case :
     return "Normal";
    case :
     return "Very Good";
    default:
     return "Normal";
   }
  }
 }

The above code is very commonly used in our projects. But here are 2 potential problems:

  • There may be more than one enum type in our project, so we may need to add a corresponding extension method for each type.
  • The code positions of the enum value and the displayed value of the enum are separated. If you look for the displayed value corresponding to the enum value, you must first find the corresponding enum extension method.

So how to improve this part of the code to eliminate the above two problems? At this time, we need to introduce the text description attribute class DescriptionAttribute in .NET.

Refactoring the code using DescriptionAttribute

In fact, a text description attribute class DescriptionAttribute has been provided in .NET. The constructor of this attribute class can receive a text description.

Next, we use DescriptionAttribute to modify the Level enum type.

 public enum Level
 {
  //Bad
  [Description("Bad")]
  B = -1,

  //Normal
  [Description("Normal")]
  N = 0,

  //Good
  [Description("Good")]
  G = 1,

  //Very Good 
  [Description("Very Good")]
  VG = 2
 }

In this way, the second problem we mentioned above is solved. Now the enum value and display value of Level enum type are encapsulated together.

So how to solve the first problem?

Here we can add an extension method for the Enum type and use reflection to read the displayed value corresponding to the current enum value.

 public static class EnumExtension
 {
  public static string ToDescription(this Enum val)
  {
   var type = ();

   var memberInfo = (());
   
   var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

   if (attributes == null ||  != 1)
   {
    //If there is no description defined, return the corresponding name of the current enum value    return ();
   }

   return (() as DescriptionAttribute).Description;
  }
 }

Since the Enum type is the base type of all enum types, this extension method can be used for all enum types.

Summarize

In this blog post, we explain how to generate text corresponding to the enum value if the .NET built-in text description attribute class DescriptionAttribute is used to generate text corresponding to the enum value. It not only reduces duplicate code, but also makes the entire enum type more cohesive.