StringEnum 0.0.2

.NET Standard 1.0
There is a newer version of this package available.
See the version list below for details.
NuGet\Install-Package StringEnum -Version 0.0.2
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 StringEnum --version 0.0.2
<PackageReference Include="StringEnum" Version="0.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StringEnum --version 0.0.2
#r "nuget: StringEnum, 0.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 StringEnum as a Cake Addin
#addin nuget:?package=StringEnum&version=0.0.2

// Install StringEnum as a Cake Tool
#tool nuget:?package=StringEnum&version=0.0.2


StringEnum is a base class for creating string-valued enums in .NET. It is just one C# file that you can copy & paste into your projects, or install via NuGet package named StringEnum.


///<completionlist cref="HexColor"/> 
class HexColor : StringEnum<HexColor>
    public static readonly HexColor Blue = New("#FF0000");
    public static readonly HexColor Green = New("#00FF00");
    public static readonly HexColor Red = New("#000FF");


  • Your StringEnum interface looks somewhat similar to a regular enum:
    // Static Parse Method
    HexColor.Parse("#FF0000") // => HexColor.Red
    HexColor.Parse("#ff0000", caseSensitive: false) // => HexColor.Red
    HexColor.Parse("invalid") // => throws InvalidOperationException

    // Static TryParse method.
    HexColor.TryParse("#FF0000") // => HexColor.Red
    HexColor.TryParse("#ff0000", caseSensitive: false) // => HexColor.Red
    HexColor.TryParse("invalid") // => null

    // Parse and TryParse returns the preexistent instances
    object.ReferenceEquals(HexColor.Parse("#FF0000"), HexColor.Red) // => true

    // Conversion from your `StringEnum` to `string`
    string myString1 = HexColor.Red.ToString(); // => "#FF0000"
    string myString2 = HexColor.Red; // => "#FF0000" (implicit cast)
  • Intellisense will suggest the enum name if the class is annotated with the xml comment <completitionlist>. (Works in both C# and VB): i.e.

Intellisense demo



  • Install StringEnum NuGet package, which is based on .Net Standard 1.0 so it runs on .Net Core >= 1.0, .Net Framework >= 4.5, Mono >= 4.6, etc.
  • Or copy StringEnum.cs base class to your project.
  • For Newtonsoft.Json serialization support, copy this extended version instead. StringEnum.cs, or follow the steps explained here

Extensibility tips

  • You may add implicit casting from string to your StringEnum, by binding the implicit cast operator to the Parse method in the target class.

    class Color : StringEnum<Color>
        public static readonly Color Blue = New("Blue");
        public static readonly Color Green = New("Greem");
        // Add the following line
        public static implicit operator Color(string stringValue) 
            => Parse(stringValue, caseSensitive: false); 
    // usage:
    Color x = "Blue"; // same as Color.Parse("Blue", caseSensitive: false)
    // or given a method:
    void PaintWall(Color wallColor)
    // you can call
  • To allow any string to be a casted as your StringEnum value, you may expose the New() method as public:

    public new static MyEnum New(string value) => StringEnum<MyEnum>.New(value);

    Or implement a special implicit cast:

    public static implicit operator MyEnum(string stringValue) => TryParse(stringValue, true) ?? New(stringValue);

    However, do not use the New() method, with user-entered strings, as they will be stored in memory in the internal ValueList cache used for Parse and TryParse.

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 netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.0 netstandard1.1 netstandard1.2 netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Universal Windows Platform uap uap10.0
Windows Phone wp8 wp81 wpa81
Windows Store netcore netcore45 netcore451
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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.0 19,832 10/23/2019
0.0.2 291 10/21/2019