EnumCraft.Core 0.2.0-preview

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

EnumCraft.Core

Type-safe enumerations and flags with rich metadata for .NET

NuGet License: MIT

What is EnumCraft?

EnumCraft provides type-safe alternatives to standard C# enums with rich metadata, compile-time safety, and powerful features. Say goodbye to magic numbers and hello to strongly-typed enumerations!

Features

TypedEnums - Smart enumerations with rich metadata
TypedFlags - Type-safe bitwise flags (like [Flags] but better)
Multiple ID Types - Support for int, long, short, Guid, string, and more
JSON Serialization - Built-in support for clean JSON output
Extensibility - Easy to extend with custom behavior
Multi-Targeted - Works with .NET Framework 4.7.2+ and .NET Standard 2.0+


Quick Start

Installation

dotnet add package EnumCraft.Core

TypedEnum Example

using EnumCraft;

public class OrderStatus : TypedEnumInt<OrderStatus>
{
    public static readonly OrderStatus Pending   = new(1, "Pending", nameof(Pending));
    public static readonly OrderStatus Confirmed = new(2, "Confirmed", nameof(Confirmed));
    public static readonly OrderStatus Shipped   = new(3, "Shipped", nameof(Shipped));
    public static readonly OrderStatus Delivered = new(4, "Delivered", nameof(Delivered));
    
    private OrderStatus(int id, string description, string code)
        : base(id, description, code) { }
}

// Usage
var status = OrderStatus.Pending;
Console.WriteLine(status.ID);          // 1
Console.WriteLine(status.Description); // "Pending"
Console.WriteLine(status.Code);        // "Pending"

// Get all values
var allStatuses = OrderStatus.GetAll();

// Get by ID
var confirmed = OrderStatus.GetByID(2);

// JSON serialization
string json = status.AsJsonString();
// {"id":1,"description":"Pending","code":"Pending"}

TypedFlags Example

using EnumCraft;

public class Permissions : TypedFlagInt<Permissions>
{
    public static readonly Permissions None    = new(0, "None", nameof(None));
    public static readonly Permissions Read    = new(1, "Read", nameof(Read));
    public static readonly Permissions Write   = new(2, "Write", nameof(Write));
    public static readonly Permissions Execute = new(4, "Execute", nameof(Execute));
    public static readonly Permissions Delete  = new(8, "Delete", nameof(Delete));
    public static readonly Permissions All     = new(15, "All", nameof(All));
    
    private Permissions(int id, string description, string code)
        : base(id, description, code) { }
}

// Usage - Combine flags with bitwise operators
var userPerms = Permissions.Read | Permissions.Write;

// Check flags
if (userPerms.HasFlag(Permissions.Read))
{
    Console.WriteLine("User can read");
}

// Check multiple flags
bool canEdit = userPerms.HasAllFlags(Permissions.Read, Permissions.Write);

// Decompose combined flags
var flags = userPerms.ToFlags();
// Returns: [Permissions.Read, Permissions.Write]

// Logical NOT
var notRead = Permissions.All & ~Permissions.Read;

Available Types

TypedEnum Variants

Type ID Type Use Case
TypedEnumInt / TypedEnumInt32 int Default choice (most common)
TypedEnumShort / TypedEnumInt16 short Compact storage
TypedEnumLong / TypedEnumInt64 long Large ID ranges
TypedEnumGuid Guid Globally unique, stable across environments
TypedEnumString string Human-readable IDs

TypedFlag Variants

Type ID Type Max Flags
TypedFlagInt / TypedFlagInt32 int 31 distinct flags
TypedFlagShort / TypedFlagInt16 short 15 distinct flags
TypedFlagLong / TypedFlagInt64 long 63 distinct flags

Why EnumCraft?

vs. Standard Enums

Standard Enum:

public enum OrderStatus
{
    Pending = 1,
    Confirmed = 2
}

// Problems:
var status = (OrderStatus)999;  // Compiles! Runtime chaos
string desc = status.ToString(); // Just "999" - not helpful

TypedEnum:

public class OrderStatus : TypedEnumInt<OrderStatus>
{
    public static readonly OrderStatus Pending = new(1, "Order Pending", nameof(Pending));
}

// Benefits:
var status = OrderStatus.GetByID(999); // Throws ArgumentOutOfRangeException
string desc = status.Description;      // "Order Pending" - rich metadata

vs. String Constants

String Constants:

public static class Status
{
    public const string Pending = "pending";
    public const string Confirmed = "confirmed";
}

// Problems:
string status = "pendin"; // Typo - compiles, fails at runtime

TypedEnum:

OrderStatus status = OrderStatus.Pending; // Type-safe, no typos possible

Advanced Features

Custom ToString Formatting

var status = OrderStatus.Pending;

status.ToString("D");  // "Order Pending" (Description)
status.ToString("C");  // "Pending" (Code)
status.ToString("I");  // "1" (ID)
status.ToString("F");  // "ID: 1, Description: Order Pending, Code: Pending"

JSON Serialization

var status = OrderStatus.Pending;

// Simple
string json = status.AsJsonString();

// With options
var options = new JsonSerializerOptions { WriteIndented = true };
string json = status.AsJsonString(options);

TypedFlags - Advanced

// Fluent API (requires EnumCraft.Extensions)
var perms = Permissions.None
    .With(Permissions.Read)
    .With(Permissions.Write)
    .Without(Permissions.Delete);

// Decomposition
var combined = Permissions.Read | Permissions.Write | Permissions.Execute;
foreach (var flag in combined.ToFlags())
{
    Console.WriteLine(flag.Code);
}
// Output: Read, Write, Execute

Framework Support

Framework Supported
.NET Framework 4.7.2+
.NET Standard 2.0+
.NET Core 3.1+
.NET 5+
.NET 6+
.NET 7+
.NET 8+ ✅ (with enhanced features)


Documentation

For comprehensive documentation, examples, and best practices, visit:


Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


License

MIT License - see LICENSE file for details


Author

Randel Bjorkquist
GitHub | NuGet

Product 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 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on EnumCraft.Core:

Package Downloads
EnumCraft.FeatureFlags

Feature Flags engine built on EnumCraft.Core.

EnumCraft.Json

JSON serialization extensions for EnumCraft.Core TypedEnum and TypedFlag instances with structured output and decomposed flags support

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0-preview 35 2/24/2026
0.1.4-preview 41 2/22/2026
0.1.3-preview 44 2/19/2026
0.1.2-preview 45 2/17/2026
0.1.1-preview 44 2/16/2026
0.1.0-preview 46 2/16/2026