StrEnum 2.0.2 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved

.NET Standard 2.0
Install-Package StrEnum -Version 2.0.2
dotnet add package StrEnum --version 2.0.2
<PackageReference Include="StrEnum" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StrEnum --version 2.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: StrEnum, 2.0.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install StrEnum as a Cake Addin
#addin nuget:?package=StrEnum&version=2.0.2

// Install StrEnum as a Cake Tool
#tool nuget:?package=StrEnum&version=2.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

StrEnum

StrEnum is a tiny library that allows to create string-based enums in C#.

Give your data more meaning by using strings over numerics while retaining type safety. Read more on when and why use string enums in my blog post.

StrEnum supports the following tools:

StrEnum targets .NET Standard 2.0 and has no external dependencies.

Installation

You can install StrEnum using the .NET CLI:

dotnet add package StrEnum

Usage

Create a class that inherits from the StringEnum<TEnum> class and use the Define method to define the members:

public class Sport : StringEnum<Sport>
{
    public static readonly Sport TrailRunning = Define("TRAIL_RUNNING");
    public static readonly Sport RoadCycling = Define("ROAD_CYCLING");
}

You can now use your string enum as you would a regular enum:

var sport = Sport.TrailRunning;

sport.ToString(); // TrailRunning

(string)sport; // TRAIL_RUNNING

sport == Sport.RoadCycling; // false

Parsing

Use the Parse method to convert the name or a string value to the corresponding string enum member:

Sport.Parse("TrailRunning"); // Sport.TrailRunning

Sport.Parse("TRAIL_RUNNING"); // Sport.TrailRunning

Sport.Parse("Quidditch"); // throws an ArgumentException: "Requested name or value 'Quidditch' was not found."

To make Parse ignore case, pass true as the second argument:

Sport.Parse("trailrunning", ignoreCase: true); // Sport.TrailRunning

Sport.Parse("trail_running", ignoreCase: true); // Sport.TrailRunning

To make Parse only match the members by value and not by name, pass MatchBy.ValueOnly as the third argument:

Sport.Parse("TRAIL_RUNNING", matchBy: MatchBy.ValueOnly); // Sport.TrailRunning

Sport.Parse("TrailRunning", matchBy: MatchBy.ValueOnly); // throws an ArgumentException: "Requested value 'TrailRunning' was not found."

Use the TryParse method when you want to check whether the provided string can be converted to a string enum:

Sport.TryParse("TrailRunning", out var trailRunning); // true

(string)trailRunning; // Sport.TrailRunning
    
Sport.TryParse("Quidditch", out var quidditch); // false

quidditch == null; // true

Listing members

Use the GetMembers method to list the members of a string enum in the order of definition:

Sport.GetMembers(); // [Sport.TrailRunning, Sport.RoadCycling]

Using switch statements

You can use StrEnum enums with switch statements with the help of C# 8 property patterns and when case guards:

switch (sport)
{
    case { } when sport == Sport.TrailRunning:
        PutOnTrailShoes();
        break;
    case { } when sport == Sport.RoadCycling:
        GetOnRoadBike();
        break;
}

You can also use switch expressions like this:

var sportName = sport switch
{
    _ when sport == Sport.TrailRunning => "Trail Running",
    _ when sport == Sport.RoadCycling => "Road Cycling",
    _ => "Not yet known"
};

Adding members after initialization

The Define method can be used to add members to a string enum after it has been initialized. Since Define is protected, you need to expose it to the client code first:

public class FictionalSport : StringEnum<FictionalSport>
{
    public static readonly FictionalSport Quidditch = Define("QUIDDITCH");

    public static FictionalSport Add(string name, string value)
    {
        return Define(value, name);
    }
}

You need to provide both name and value for the members defined in such way:

var podracing = FictionalSport.Add("Podracing", "PODRACING");

(string)podracing; // PODRACING

FictionalSport.Parse("Podracing").ToString(); // Podracing

License

Copyright © 2022 Dmitry Khmara.

StrEnum is licensed under the MIT license.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on StrEnum:

Package Downloads
StrEnum.EntityFrameworkCore The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

String enum support for EF Core

StrEnum.System.Text.Json The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

String enum support for System.Text.Json.

StrEnum.AspNetCore The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

String enum support for ASP.NET Core.

StrEnum.Dapper The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

String enum support for Dapper.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.2 32 8/1/2022
2.0.1 48 7/29/2022
2.0.0 62 7/29/2022
1.5.2 42 7/26/2022
1.5.1 89 5/19/2022
1.5.0 92 5/18/2022
1.4.0 106 5/14/2022
1.3.0 98 5/11/2022
1.2.2 54 5/5/2022
1.2.1 91 4/10/2022
1.2.0 91 4/9/2022