LinkDotNet.Enumeration 1.3.1

dotnet add package LinkDotNet.Enumeration --version 1.3.1
                    
NuGet\Install-Package LinkDotNet.Enumeration -Version 1.3.1
                    
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="LinkDotNet.Enumeration" Version="1.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LinkDotNet.Enumeration" Version="1.3.1" />
                    
Directory.Packages.props
<PackageReference Include="LinkDotNet.Enumeration" />
                    
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 LinkDotNet.Enumeration --version 1.3.1
                    
#r "nuget: LinkDotNet.Enumeration, 1.3.1"
                    
#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 LinkDotNet.Enumeration@1.3.1
                    
#: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=LinkDotNet.Enumeration&version=1.3.1
                    
Install as a Cake Addin
#tool nuget:?package=LinkDotNet.Enumeration&version=1.3.1
                    
Install as a Cake Tool

Enumeration

.NET Nuget GitHub tag

Source code generated string Enumeration with completeness!

What is in the box?

This source code generator let's you easily create string based enumerations with a lot of features.

[Enumeration("Red", "Green", "Blue")]
public sealed partial record Color;

That's all you need to do to create a string based enumeration. You can either use it like this:

var color = Color.Red;

// Create it by a string key:
var color = Color.Create("Red");

Exhaustiveness

The great benefit of the library is that you have support for exhaustiveness:


var color = Color.Create("Red");

color.Match(
    red => Console.WriteLine("It's red!"),
    green => Console.WriteLine("It's green!"),
    blue => Console.WriteLine("It's blue!")
);

Or return a value:

var color = Color.Create("Red");

var colorCode = color.Match(
    red => "#FF0000",
    green => "#00FF00",
    blue => "#0000FF"
);

Controlling field names

The Enumeration attribute accepts an optional first argument of type Casing to control how the static member names are derived from the string values. By default, the library uses PascalCase to convert string values into member names. If you want to preserve the original casing of the string values, you can set the Casing to Preserve.

[Enumeration(Casing.Preserve, "red", "green", "blue")]
public sealed partial class Color;

Generates:

public sealed partial class Color
{
    public static readonly Color red = new("red");
    public static readonly Color green = new("green");
    public static readonly Color blue = new("blue");
    // ...

The default is PascalCase to feel "natural" to C# developers.

Creating from a string key

Two methods are exposed Create and TryCreate to create an instance of the enumeration from a string key. The Create method will throw an ArgumentException if the key is not valid, while the TryCreate method will return a boolean indicating whether the creation was successful and output the created value through an out parameter.

var color = Color.Create("Red"); // Throws if "Red" is not a valid key

if (Color.TryCreate("Red", out var color))
{
    // Use color
}
else
{
    // Handle invalid key
}

Parsing (IParsable)

The generated types implement IParsable<T> and ISpanParsable<T>, making them compatible with modern .NET features like Minimal APIs and Model Binding.

if (Color.TryParse("Red", null, out var color))
{
    // Use color
}

Implicit/Explicit Conversions

Instances of the generated enumeration can be implicitly converted to strings (returns the Key property), and strings can be explicitly converted back to the enumeration type (calls Create).

string colorKey = Color.Red; // Implicit conversion
var color = (Color)"Green"; // Explicit conversion

Equality and Comparison

Records implement value-based equality by default. For classes, IEquatable<T> is automatically implemented, comparing the Key property.

var isRed = Color.Red == "Red"; // String comparison
var areEqual = Color.Red.Equals(Color.Create("Red")); // Value equality

Getting all values

Calling All will return a collection of all possible values. This is implemented using a FrozenSet to ensure immutability and thread-safety.

Limitations

  • Your code should run at least net8.0 or later, as the library uses things like FrozenSet.
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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.3.1 82 4/6/2026
1.3.0 81 4/6/2026
1.2.0 81 4/6/2026
1.1.0 77 4/6/2026
1.0.1 80 4/5/2026
1.0.0 79 4/5/2026