AeonSake.Extensions.Enums 1.2.0

dotnet add package AeonSake.Extensions.Enums --version 1.2.0
                    
NuGet\Install-Package AeonSake.Extensions.Enums -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AeonSake.Extensions.Enums" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AeonSake.Extensions.Enums" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="AeonSake.Extensions.Enums" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AeonSake.Extensions.Enums --version 1.2.0
                    
#r "nuget: AeonSake.Extensions.Enums, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AeonSake.Extensions.Enums@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AeonSake.Extensions.Enums&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=AeonSake.Extensions.Enums&version=1.2.0
                    
Install as a Cake Tool

Enum Extensions

This library contains a small collection of extension methods for enums. Also contains an optional source-generator for faster extension calls.

Installation

The library is available as NuGet package on nuget.org and this project's GitLab Package Registry.

Features

Wrapper/Polyfill Methods

The library provides generic wrappers and polyfill implementations for several Enum class methods, usable directly on the type or instance.

using AeonSake.Extensions.Enums;

public enum MyEnum
{
    Value1 = 1,
    Value2 = 2
}

MyEnum.GetValues(); //returns [MyEnum.Value1, MyEnum.Value2]
MyEnum.GetNames();   //returns ["Value1", "Value2"]

MyEnum.Value1.IsDefined(); //returns true
((MyEnum) 99).IsDefined(); //returns false

MyEnum.Value1.GetName(); //returns "Value1"

Flag Methods

The library also contains several extension methods to improve the handling of flagged enums.

using AeonSake.Extensions.Enums;

[Flags]
public enum MyEnum
{
    Value0 = 0,
    Value1 = 1,
    Value2 = 2,
    Value3 = 4,
    Value2And3 = Value2 | Value3
}

MyEnum.GetFlagValues(); //returns [MyEnum.Value1, MyEnum.Value2, MyEnum.Value3]

(MyEnum.Value1|MyEnum.Value2).GetFlags(); //returns [MyEnum.Value1, MyEnum.Value2]
MyEnum.Value2And3.GetFlags();             //returns [MyEnum.Value2, MyEnum.Value3]
((MyEnum) 5).GetFlags();                  //returns [MyEnum.Value1, MyEnum.Value3]

Other Methods

using System.ComponentModel.DataAnnotations;
using AeonSake.Extensions.Enums;

public enum MyEnum
{
    [Display(Name = "Value 1")]
    Value1 = 1,
    Value2 = 2
}

MyEnum.Value1.ToValue(); //returns 1 (as ulong)

MyEnum.Value1.GetDisplayName(); //returns "Value 1"
MyEnum.Value2.GetDisplayName(); //returns "Value2"
((MyEnum) 99).GetDisplayName(); //returns "99"

Source-Generator

The library also includes a source-generator to pre-compile the content of all mentioned extension methods. This drastically improves speed, decreases memory allocation, and avoids any use of reflection. The cost is the additional generated code alongside your enum type definitions. Just apply the GeneratedEnumExtensionsAttribute to the enum type you wish to pre-compile. The source-generator will also recognize, if an enum has FlagsAttribute set and generates extension methods for flagged enums accordingly.

[GeneratedEnumExtensions]
public enum MyEnum
{
    Value1 = 1,
    Value2 = 2
}

Source-generated extension methods come with a few limitations:

  • Enum types must be public or internal
  • If the enum type is nested in another class, the overall accessibility to the enum type must still be public or internal (i.e., the container class cannot be less accessible than that)
  • Enum types cannot be nested in a generic class
Benchmarks
BenchmarkDotNet v0.15.8, Windows 10 (10.0.19045.6466/22H2/2022Update)
Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical cores
.NET SDK 10.0.203
  [Host]     : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3
  DefaultJob : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3
Method Mean Error StdDev Ratio Gen0 Allocated Alloc Ratio
GetValues 24.444 ns 0.0600 ns 0.0501 ns 1.00 0.0076 48 B 1.00
GetValues_Gen 5.103 ns 0.1195 ns 0.1118 ns 0.21 0.0076 48 B 1.00
GetFlagValues 21.479 ns 0.1847 ns 0.1727 ns 1.00 0.0179 112 B 1.00
GetFlagValues_Gen 4.976 ns 0.1000 ns 0.0936 ns 0.23 0.0064 40 B 0.36
GetNames 15.930 ns 0.1071 ns 0.0894 ns 1.00 0.0102 64 B 1.00
GetNames_Gen 6.215 ns 0.1394 ns 0.1304 ns 0.39 0.0102 64 B 1.00
IsDefined 1.4810 ns 0.0080 ns 0.0071 ns 1.000 - - NA
IsDefined_Gen 0.0001 ns 0.0002 ns 0.0001 ns 0.000 - - NA
GetName 2.4806 ns 0.0017 ns 0.0014 ns 1.00 - - NA
GetName_Gen 0.4395 ns 0.0017 ns 0.0013 ns 0.18 - - NA
GetDisplayName 714.2823 ns 8.1999 ns 7.2690 ns 1.000 0.0839 528 B 1.00
GetDisplayName_Gen 0.4210 ns 0.0016 ns 0.0013 ns 0.001 - - 0.00
ToValue 0.0001 ns 0.0002 ns 0.0002 ns ? - - ?
ToValue_Gen 0.0000 ns 0.0000 ns 0.0000 ns ? - - ?
GetFlags 20.402 ns 0.2281 ns 0.2134 ns 1.00 0.0166 104 B 1.00
GetFlags_Gen 6.270 ns 0.0466 ns 0.0413 ns 0.31 0.0051 32 B 0.31

Building from Source

Building this project requires .NET 5.0 SDK or newer, or .NET Core 3.0 or newer. Refer to the full list of supported frameworks to see more.
Some packages are loaded from external sources (nuget.org) and are required for compilation. Make sure to run nuget restore (or Restore NuGet packages from inside Visual Studio) before building.

License

The project is licensed under MIT.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 92 5/5/2026
1.1.0 93 5/5/2026
1.0.0 86 5/2/2026