StrEnum 1.2.0

.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
NuGet\Install-Package StrEnum -Version 1.2.0
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.
dotnet add package StrEnum --version 1.2.0
<PackageReference Include="StrEnum" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StrEnum --version 1.2.0
#r "nuget: StrEnum, 1.2.0"
#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=1.2.0

// Install StrEnum as a Cake Tool
#tool nuget:?package=StrEnum&version=1.2.0

StrEnum

StrEnum is a tiny library that allows to create string-based enums in C#. Using strings instead of numerics gives your state more meaning when it crosses the boundaries of your .NET application.

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

How do I install it?

You can install StrEnum using the .NET CLI:

dotnet add package StrEnum

Or using the Package Manager console:

PM> Install-Package StrEnum

How do I use it?

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

public class Country : StringEnum<Country>
{
    public static readonly Country Ukraine = Define("UKR");
    public static readonly Country SouthAfrica = Define("ZAF");
}

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

var country = Country.Ukraine;

country.ToString(); // Ukraine

(string)country; // UKR

country == Country.SouthAfrica; // false

Parsing

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

Country.Parse("Ukraine"); // Country.Ukraine

Country.Parse("UKR"); // Country.Ukraine

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

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 FictionalCountry : StringEnum<FictionalCountry>
{
    public static readonly FictionalCountry Gondor = Define("GDR");

    public static FictionalCountry Add(string name, string code)
    {
        return Define(code, name);
    }
}

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

var rohan = FictionalCountry.Add("Rohan", "RHN");

(string)rohan; // RHN

FictionalCountry.Parse("Rohan").ToString(); // Rohan

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 60 8/1/2022
2.0.1 69 7/29/2022
2.0.0 105 7/29/2022
1.5.2 62 7/26/2022
1.5.1 103 5/19/2022
1.5.0 110 5/18/2022
1.4.0 129 5/14/2022
1.3.0 120 5/11/2022
1.2.2 68 5/5/2022
1.2.1 104 4/10/2022
1.2.0 113 4/9/2022