LinkDotNet.Enumeration
1.3.1
dotnet add package LinkDotNet.Enumeration --version 1.3.1
NuGet\Install-Package LinkDotNet.Enumeration -Version 1.3.1
<PackageReference Include="LinkDotNet.Enumeration" Version="1.3.1" />
<PackageVersion Include="LinkDotNet.Enumeration" Version="1.3.1" />
<PackageReference Include="LinkDotNet.Enumeration" />
paket add LinkDotNet.Enumeration --version 1.3.1
#r "nuget: LinkDotNet.Enumeration, 1.3.1"
#:package LinkDotNet.Enumeration@1.3.1
#addin nuget:?package=LinkDotNet.Enumeration&version=1.3.1
#tool nuget:?package=LinkDotNet.Enumeration&version=1.3.1
Enumeration
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.0or later, as the library uses things likeFrozenSet.
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.