StrEnum 2.0.4

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

StrEnum

StrEnum is a tiny library for creating string-based enums in C#. Use strings instead of integers for richer, more meaningful data while keeping type safety. Read more about when and why to use string enums in this blog post.

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

Ecosystem

These companion packages plug StrEnum into the rest of the .NET ecosystem:

Installation

Install StrEnum via the .NET CLI:

dotnet add package StrEnum

Usage

Defining a string enum

Inherit from StringEnum<TEnum> and use Define to declare members:

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

The field name becomes the member's name; the argument to Define becomes its value.

Working with members

Members behave like a regular enum — including ToString, casting, and equality:

var sport = Sport.TrailRunning;

sport.ToString();           // "TrailRunning"
(string)sport;              // "TRAIL_RUNNING"
sport == Sport.RoadCycling; // false

Parsing

Parse converts a name or value into the matching member, throwing if no match is found:

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

Pass ignoreCase: true for case-insensitive matching:

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

Pass MatchBy.ValueOnly to match by value and skip member names:

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

Use TryParse for the non-throwing variant:

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

Listing members

GetMembers returns members in declaration order:

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

Switch statements and expressions

Because string enum members aren't constants, use property patterns with when guards:

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

The same pattern works in switch expressions:

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

Adding members at runtime

Define is protected, but you can expose it through a public method to add members after the class is initialized:

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

    public static FictionalSport Add(string name, string value) => Define(value, name);
}

Both name and value must be supplied for members added this way:

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

(string)podracing;                            // "PODRACING"
FictionalSport.Parse("Podracing").ToString(); // "Podracing"

License

Copyright © 2026 Dmytro Khmara.

StrEnum is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on StrEnum:

Package Downloads
StrEnum.System.Text.Json

String enum support for System.Text.Json.

StrEnum.EntityFrameworkCore

String enum support for EF Core

StrEnum.AspNetCore

String enum support for ASP.NET Core.

StrEnum.Dapper

String enum support for Dapper.

ClickTime.NET

Wrapper for API @ https://developer.clicktime.com/docs/api/

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.4 96 4/25/2026
2.0.3 877 7/7/2025
2.0.2 4,811 8/1/2022
2.0.1 616 7/29/2022
2.0.0 6,164 7/29/2022
1.5.2 573 7/26/2022
1.5.1 583 5/19/2022
1.5.0 850 5/18/2022
1.4.0 1,325 5/14/2022
1.3.0 1,118 5/11/2022
1.2.2 567 5/5/2022
1.2.1 618 4/10/2022
1.2.0 1,077 4/9/2022