StringEnum 0.0.2

StringEnum is a base class for creating string-valued enums in .NET.

Features
- Your StringEnum interface looks somewhat similar to a regular enum:
- Intellisense will suggest the enum name if the class is annotated with the xml comment `<completitionlist>`. (Works in both C# and VB)

Usage:

///<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");
}

// 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

// Conversion from your `StringEnum` to `string`
string myString1 = HexColor.Red.ToString(); // => "#FF0000"
string myString2 = HexColor.Red; // => "#FF0000" (implicit cast)

There is a newer version of this package available.
See the version list below for details.
Install-Package StringEnum -Version 0.0.2
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

StringEnum

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.

Usage:

///<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");
}

Features

  • 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 &lt;completitionlist&gt;. (Works in both C# and VB): i.e.

Intellisense demo

Instalation

Either:

  • 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
    PaintWall("Blue");
    
  • 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.

StringEnum

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.

Usage:

///<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");
}

Features

  • 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 &lt;completitionlist&gt;. (Works in both C# and VB): i.e.

Intellisense demo

Instalation

Either:

  • 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
    PaintWall("Blue");
    
  • 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.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.1.0 88 10/23/2019
0.0.2 53 10/21/2019