NDepend Blog

Improve your .NET code quality with NDepend

Fastest C# Enum to String

July 15, 2024 3 minutes read

Fastest C# Enum to String

This article benchmarks various ways to obtain a string from an enumeration value in C# .NET, along with other common enumeration APIs.

At NDepend, we value your time, so here are our key findings:

  • The library NetEscapades.EnumGenerators generates code at compile time for faster enumeration APIs, including enum to string conversions. This generated code outperforms standard .NET enumeration APIs and other libraries benchmarked here. To use it, just set the attribute [EnumExtensions] on each enum declaration, and a class named EnumNameExtensions in the anonymous namespace is generated for each enum.
  • If the enumeration value is known at compile time, nameof(MyEnum.MyValue) prompts the compiler to emit the string directly, making it the fastest option available in this situation.
  • .NET 8 brings substantial performance optimizations for enumerations. If you haven’t updated yet, we highly recommend doing so. However, the code generated by NetEscapades.EnumGenerators still outperforms .NET 8 optimizations.
  • If you prefer not to generate enum extension code at compile time and instead favor dynamic code, consider using Enums.NET for faster enum-to-string conversions and FastEnum for quicker string-to-enum parsing. Both libraries outperform .NET 8 enum APIs in these specific operations.

The Benchmark

Let’s benchmark these enum APIs:

To do so we rely on BenchmarkDotNet. The .csproj project file looks like this:

Here is the benchmark C# source code:

Here are the results:

The outcomes are:

  • nameof(MyEnum.MyValue) is the fastest method. This isn’t surprising since the compiler directly emits the string "Saturday" without requiring a runtime lookup. However, if we use nameof(_day), the compiler emits the string "_day", which is the name of the variable, not the name of the day that the variable _day references.
  • To obtain a string dynamically from an enum value, both Meziantou.Framework.FastEnumToStringGenerator and NetEscapades.EnumGenerators are slightly better than other libraries. Both packages emit this code at compile time:

  • On the other hand, both Enums.NET and FastEnum don’t emit code. Their strategy is to cache the values in memory at runtime.
  • Meziantou.Framework.FastEnumToStringGenerator doesn’t generate code for other enumeration APIs.
  • NetEscapades.EnumGenerators is the fastest to parse a string to attempt to obtain an enumeration value.
  • NetEscapades.EnumGenerators is the fastest for IsDefined(). It generates an unsafe and generic method that relies on System.RuntimeType.
  • FastEnum is the fastest for both GetNames() and GetValues(). It caches in memory the array while NetEscapades.EnumGenerators build a new array at each invocation.

.NET 8.0 improvements compared to .NET 7.0

Let’s benchmark .NET 8.0 improvements against .NET 7.0:

This is the .csproj file content:

Here is the benchmark source code.

Noteworthy NetEscapades.EnumGenerators Features

Given that the benchmark results recommend using NetEscapades.EnumGenerators, here are some of its interesting features:

  • NetEscapades.EnumGenerators generates a TryParse(...) overload with bool ignoreCase and allowMatchingMetadataAttribute:

  • When you create an enum decorated with the [Flags] attribute, an additional method is generated that offers a bitwise alternative to the Enum.HasFlag(flag) method.