Monday, July 9, 2018

Getting Enum descriptions using C#

Accessing Enum descriptions isn't complicated. Let's review an elegant solution using attributes, extension methods and C#.

Introduction

One programming data type that developers use a lot are Enums. Enums are simple and when well utilized, simplify the development making your code cleaner. In .NET, all Enums derive from System.Enum. In this post we assume you know and work with enums already. if not, check this link out for a nice introduction.

A common question I see is: how to build and access enum descriptions? There's different answers for that question that depend on which context your code is running. In this post we'll examine how to do it in C# using attributes and extension methods. We will also demo it using a console app but the same concept could be used on your backend, a windows service, a library, etc.

The DescriptionAttribute

The secret do do this the right way is using the DescriptionAttribute available on the System.ComponentModel.DescriptionAttribute class. For illustration, let's assume that we have a Status Enum in our code base that determines the possible statuses invoices in our system may have. In theory, it could look like:

Note that From the above code you can see that I added a Description attribute decorating each of my enums with a description string associated to them. That's exactly what that attribute is for. But how do we access the attribute value?

The EnumHelper.GetDescription extension method

The second part of the solution consists in using the extension method below so that access to that method is centralized in one place, remains simple and can be reused throughout our application:


Accessing the Enum Description

Now the last part. To access your description, you just have to call the GetDescription extention method to get that attribute as a string. Note that on the example below, I'm also doing two interesting things:
  1. Overriding the ToString method so I can simply call 
  2. Using String Interpolation to simplify the formatting instead of String.Format

Those are usually good practices that you should adhere to.


Example

To finish off, let's build a simple .NET Core tool to practice the above code. Oh, and because we're using .NET Core, let's run it on Windows and on my beloved Fedora Linux.

Running on Windows

Here's it our code running on Windows:

Running on Linux

And, as a nice exercise, let's run it on Linux too. First, we need to run the dotnet publish command to build it:
Then, just run it on your Linux with dotnet EnumDescription.dll:

Final Thoughts

On this post we reviewed how to combine the use the DescriptionAttribute class and a C# extension method to decorate/access your enums with nice contextual descriptions. You also learned how to extend your entities to use it in a simple demo console application on Windows and Linux. Remember that this technique can be used everywhere. Just use your creativity!

The source used here can be found on GitHub.

See Also

.NET Core and .NET merging as .NET 5.0
Creating a Ubuntu Desktop instance on Azure
Windows Subsystem for Linux, the best way to learn Linux on Windows
Copying data between Azure databases in 3 easy steps
Simplifying Razor logic with C# Local Functions in ASP.NET Core
Why you should start using .NET Core
Building and Running Asp.Net Core apps on LinuxGenerating views in the backend with ASP.Net and ASP.Net Core

For more posts on .Net Core, please click here.