StrEnum 1.2.1
Prefix ReservedSee the version list below for details.
dotnet add package StrEnum --version 1.2.1
NuGet\Install-Package StrEnum -Version 1.2.1
<PackageReference Include="StrEnum" Version="1.2.1" />
paket add StrEnum --version 1.2.1
#r "nuget: StrEnum, 1.2.1"
// Install StrEnum as a Cake Addin #addin nuget:?package=StrEnum&version=1.2.1 // Install StrEnum as a Cake Tool #tool nuget:?package=StrEnum&version=1.2.1
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.
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 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. |
.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. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (5)
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. |
|
ClickTime.NET
Wrapper for API @ https://developer.clicktime.com/docs/api/ |
|
StrEnum.Dapper
String enum support for Dapper. |
GitHub repositories
This package is not used by any popular GitHub repositories.