FastEnum 2.0.0-preview3

This is a prerelease version of FastEnum.
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package FastEnum --version 2.0.0-preview3                
NuGet\Install-Package FastEnum -Version 2.0.0-preview3                
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="FastEnum" Version="2.0.0-preview3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FastEnum --version 2.0.0-preview3                
#r "nuget: FastEnum, 2.0.0-preview3"                
#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.
// Install FastEnum as a Cake Addin
#addin nuget:?package=FastEnum&version=2.0.0-preview3&prerelease

// Install FastEnum as a Cake Tool
#tool nuget:?package=FastEnum&version=2.0.0-preview3&prerelease                

FastEnum

FastEnum is extremely fast enum utilities for C#/.NET. It's much faster than .NET. Provided methods are all achieved zero allocation and are designed easy to use like System.Enum. This library is quite useful to significantly improve your performance because enum is really popular feature.

Releases

Performance

Benchmark

BenchmarkDotNet v0.14.0, Windows 11 (10.0.22621.4037/22H2/2022Update/SunValley2)
13th Gen Intel Core i7-1360P, 1 CPU, 16 logical and 12 physical cores
.NET SDK 8.0.400
  [Host]     : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2
  Job-CYQAVK : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2

[!NOTE] At present, FastEnum doesn't generate source code at compile time. We understand that leveraging Roslyn's Source Generator functionality could further accelerate performance, and thus our current implementation is not theoretically the "fastest" possible. We are positively considering the introduction of Source Generator in the future. However, as we aim to maintain a library that is both user-friendly and easily comprehensible, we may need to wait for future enhancements in C#'s expressiveness. We appreciate your understanding in this matter.

Support Platform

  • .NET 8.0+

[!Important] For those who wish to use this on older platforms, please use v1.8.0. It supports follows.

  • .NET Framework 4.6.1+
  • .NET Standard 2.0+
  • .NET 5+

How to use

This library super easy to use like System.Enum that is standard of .NET. Look below:

//--- FastEnum
var values = FastEnum.GetValues<Fruits>();
var names = FastEnum.GetNames<Fruits>();
var name = FastEnum.GetName<Fruits>(Fruits.Apple);
var toString = Fruits.Apple.FastToString();
var defined = FastEnum.IsDefined<Fruits>(123);
var parse = FastEnum.Parse<Fruits>("Apple");
var tryParse = FastEnum.TryParse<Fruits>("Apple", out var value);
//--- .NET
var values = Enum.GetValues(typeof(Fruits)) as Fruits[];
var names = Enum.GetNames(typeof(Fruits));
var name = Enum.GetName(typeof(Fruits), Fruits.Apple);
var toString = Fruits.Apple.ToString();
var defined = Enum.IsDefined(typeof(Fruits), 123);
var parse = Enum.Parse<Fruits>("Apple");
var tryParse = Enum.TryParse<Fruits>("Apple", out var value);

As you can see, the replacement from System.Enum is very easy. You never confuse.

More features

There are some functions that are often used for enum, and you can be used more conveniently by including them together.

1. Gets pairwised member information

Sometimes you want name / value pair of enum. Member<TEnum> can be used under such cases. Of course supports deconstruction feature. FieldInfo is also included, so please use it for reflection code.

class Member<TEnum>
{
    public TEnum Value { get; }
    public string Name { get; }
    public FieldInfo FieldInfo { get; }
    // etc...
}

var member = Fruits.Apple.ToMember()!;
var (name, value) = member;  // Supports deconstruction

2. Gets EnumMemberAttribute.Value

I often see the developer using EnumMemberAttribute as an alias for field name. So FastEnum provides an API that the value can be quickly obtained from the EnumMemberAttribute.Value property.

enum Company
{
    [EnumMember(Value = "Apple, Inc.")]
    Apple = 0,
}

var value = Company.Apple.GetEnumMemberValue();  // Apple, Inc.

3. Adds multiple label annotations to a field

Multiple attributes can’t be attached to the same field, since EnumMemberAttribute is specified AllowMultiple = false. It’s inconvenient and I don’t like it personally, so I often use my own LabelAttribute as an alternative. You can use it conveniently as follows, because FastEnum provides this feature.

enum Company
{
    [Label("Apple, Inc.")]
    [Label("AAPL", 1)]
    Apple = 0,
}

var x1 = Company.Apple.GetLabel();   // Apple, Inc.
var x2 = Company.Apple.GetLabel(1);  // AAPL

Limitation

1. Provides only generics API

FastEnum provides only generics version method because of performance reason. System.Enum provides System.Type argument overload, but that’s too slow because of boxing occuration. If you need to use the method that passes System.Type type, please use System.Enum version.

2. Can’t parse comma-separated string

System.Enum.Parse can parse like following string. I think that it isn’t well known because it is a specification that exists quietly.

//--- Assuming there is an enum type like following...
[Flags]
enum Fruits
{
    Apple = 1,
    Lemon = 2,
    Melon = 4,
    Banana = 8,
}

//--- Passes comma-separated string
var value = Enum.Parse<Fruits>("Apple, Melon");
Console.WriteLine((int)value);  // 5

It seems to be a useful function when performing flag processing, but if tries to add such a comma-separated analysis, the overhead will come out, so cutting this feature off makes speed up. I think that in most cases there is no problem, because this feature is rarely used (at least I have NEVER used for 12 years).

Why fast ?

As you might expect, it’s because cached internally. It takes the approach of Static Type Caching, so the reading cost is almost zero. Based on this, I use techniques for avoiding allocation, and create specialized dictionary for specific key internally.

Installation

Getting started from downloading NuGet package.

dotnet add package FastEnum

License

This library is provided under MIT License.

Author

Takaaki Suzuki (a.k.a @xin9le) is software developer in Japan who awarded Microsoft MVP for Developer Technologies (C#) since July 2012.

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on FastEnum:

Package Downloads
Stringier

Text extensions for a more pleasant text processing experience.

Defender

Package Description

Shinya.Core

Shinya.Framework

LShared.Frameworks

Package Description

BimLab.PikTools.Updater.Sdk

PikTools Updater SDK

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on FastEnum:

Repository Stars
TylerBrinkley/Enums.NET
Enums.NET is a high-performance type-safe .NET enum utility library
NethermindEth/nethermind
A robust execution client for Ethereum node operators.
Version Downloads Last updated
2.0.5 20,226 1/13/2025
2.0.4 6,819 12/26/2024
2.0.3 5,157 12/12/2024
2.0.2 93,422 11/15/2024
2.0.1 25,761 9/29/2024
2.0.0 2,288 9/19/2024
1.8.0 1,091,041 6/28/2022
1.7.0 102,158 11/9/2021
1.6.1 60,612 5/5/2021
1.6.0 137,738 11/14/2020
1.5.4 2,621 11/11/2020
1.5.3 15,100 4/18/2020
1.5.2 8,859 4/14/2020
1.5.1 8,829 1/12/2020
1.5.0 2,610 1/12/2020
1.4.1 3,804 10/30/2019
1.4.0 2,591 10/30/2019
1.3.1 2,761 10/15/2019
1.3.0 2,646 9/28/2019
1.2.0 2,614 9/16/2019
1.1.0 2,892 9/9/2019
1.0.0 2,964 9/6/2019