EnumConverter 2.2.0

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

EnumConverter

Simple extension methods for converting between enum values, numeric identifiers, and string representations.

Features

  • Convert enums to their numeric underlying values with null checking.
  • Generic helpers cover every enum backing type (byte, long, etc.).
  • Definition validation understands [Flags] combinations while rejecting unknown bits.
  • Parse from strings using StringComparison or allocation-free ReadOnlySpan<char> APIs.
  • Nullable and try-style helpers cover numeric, string, and span inputs.
  • Built for modern .NET 8 apps.

Installation

Install from NuGet:

 dotnet add package EnumConverter --version 2.2.0

Usage

using System;
using EnumConverter;

[Flags]
public enum AccessRights : byte
{
    None = 0,
    Read = 1,
    Write = 2
}

public enum OrderStatus
{
    Pending = 1,
    Shipped = 2,
    Delivered = 3
}

// Numeric conversions
int value = OrderStatus.Shipped.ToInt();              // 2
OrderStatus parsed = value.ToEnum<OrderStatus>();     // OrderStatus.Shipped
byte access = AccessRights.Read.ToValue<AccessRights, byte>(); // 1

// Validation across all backing types (supports [Flags] combinations)
int unknown = 999;
unknown.ToEnum<OrderStatus>(validateDefinition: true); // throws ArgumentOutOfRangeException
var readWrite = ((byte)3).ToEnum<AccessRights, byte>(validateDefinition: true); // Read | Write

// Nullable numbers
int? maybeValue = null;
OrderStatus? nullableStatus = maybeValue.ToEnum<OrderStatus>();
if (maybeValue.TryToEnum(out OrderStatus fallback))
{
    // handled
}

// String helpers with custom comparisons
var fromString = "delivered".ToEnum<OrderStatus>();
if (" shipped ".TryToEnum<OrderStatus>(out var statusFromString, StringComparison.OrdinalIgnoreCase))
{
    // use parsed value
}

// Allocation-free span parsing
ReadOnlySpan<char> span = "Pending".AsSpan();
var spanParsed = span.ToEnum<OrderStatus>();

// Graceful fallbacks
var optionalStatus = "unknown".ToNullableEnum<OrderStatus>();          // null
var optionalFlags = "read, execute".ToNullableEnum<AccessRights>();    // AccessRights.Read | AccessRights.Execute

Requirements

  • .NET SDK 8.0 or later

Building & Testing

  1. Restore and test the solution:
 dotnet restore
 dotnet test

Contributing

Issues and pull requests are welcome. Please open an issue to discuss significant changes before submitting a PR.

License

Released under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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
2.2.0 105 9/26/2025
2.1.0 101 9/26/2025
2.0.0 111 9/26/2025
1.1.0 106 9/26/2025

Added generic backing-type conversions, improved [Flags] validation, new string/span parsing helpers, simplified namespace usage, and introduced a compatibility facade for the legacy namespace.