ZeroAlloc.StateMachine 1.6.5

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

ZeroAlloc.StateMachine

NuGet Build License: MIT AOT GitHub Sponsors

Source-generated finite state machines for .NET, a compile-time alternative to Stateless: transitions are a generated switch, 0 B per transition and NativeAOT-ready.

Why ZeroAlloc.StateMachine

  • 0 B per transition, asserted in CI. TryFire allocates nothing on valid, invalid, guarded and concurrent transitions; AllocationTests fails CI if it does. Stateless allocates 24 B just to reject an invalid trigger.
  • About 15× faster than Stateless. Rejecting an invalid trigger takes 1.6 ns against 23 ns in the same run, and a three-step cycle with reset is about 300× faster.
  • Works under NativeAOT and trimming. No reflection; every PR publishes an AOT sample with PublishAot=true.
  • Compile-time diagnostics instead of runtime surprises. An unreachable state, a state with no way out or a likely trigger typo is reported at build time, from ZSM0001 to ZSM0026.

Install

dotnet add package ZeroAlloc.StateMachine

Quick start

using ZeroAlloc.StateMachine;

var order = new OrderMachine();
Console.WriteLine(order.TryFire(Trigger.Submit)); // True: Idle -> Pending
Console.WriteLine(order.Current);                 // Pending
Console.WriteLine(order.TryFire(Trigger.Pay));    // True: Pending -> Done
Console.WriteLine(order.TryFire(Trigger.Submit)); // False: Done is terminal

public enum State { Idle, Pending, Done }
public enum Trigger { Submit, Pay }

[StateMachine(InitialState = nameof(State.Idle))]
[Transition<State, Trigger>(From = State.Idle, On = Trigger.Submit, To = State.Pending)]
[Transition<State, Trigger>(From = State.Pending, On = Trigger.Pay, To = State.Done)]
[Terminal<State>(State = State.Done)]
public partial class OrderMachine
{
    partial void OnEnterDone(State from) => Console.WriteLine($"Order complete, came from {from}");
}

The generator writes TryFire as a switch over (State, Trigger) and calls the OnEnter… and OnExit… hooks you implement.

What you get

  • Guards that block a transition at runtime through a generated partial bool method.
  • Entry and exit hooks per state.
  • Concurrent mode: lock-free, thread-safe transitions with one attribute property.
  • Struct machines that keep even the machine itself off the heap.
  • Composite and history states, timeout transitions, and concurrent parts that host several machines in one class.
  • Mermaid diagram export as a generated constant.

Every attribute and property is in the attribute reference.

Performance

BenchmarkDotNet, same run against Stateless 5.20.1. Allocations are exact.

Operation Speed-up vs Stateless Stateless alloc ZA alloc
Fire valid, 3-step cycle with reset ~300× 7,600 B 0 B
Fire invalid ~15× 24 B 0 B
Guard blocked > 400× 768 B 0 B

The cycle row includes each library's reset: a field write for ZeroAlloc, a fluent reconfiguration for Stateless. The guard row is a lower bound, because ZeroAlloc's time is below what BenchmarkDotNet can measure. Full numbers and methodology: docs/performance.md.

Learn more

License

MIT. See 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 is compatible.  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 is compatible.  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.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on ZeroAlloc.StateMachine:

Package Downloads
ZeroAlloc.Resilience

Source-generated, zero-allocation resilience policies for .NET. Add [Retry], [Timeout], [RateLimit], and [CircuitBreaker] to an interface; the generator emits a proxy composing all policies in declaration order. AOT-safe.

ZeroAlloc.Scheduling

Source-generated zero-allocation background job scheduling for .NET.

ZeroAlloc.Outbox

Source-generated transactional outbox for .NET.

Thalos.NET

Thalos.NET agent runtime on Microsoft Agent Framework: agent factory, tool catalog with authorization, session state machine, in-memory session store.

ZeroAlloc.EventSourcing.Aggregates

Aggregate base + generator-emitted Apply dispatch for ZeroAlloc.EventSourcing.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.6.5 27 10/16/2026
1.6.4 987 10/15/2026
1.6.3 6,248 10/11/2026
1.6.2 12,035 9/30/2026
1.6.1 6,386 9/28/2026
1.6.0 2,023 9/27/2026
1.5.4 7,455 9/20/2026
1.5.3 40,790 9/19/2026
1.5.2 1,464 8/8/2026
1.5.1 596 5/25/2026
1.5.0 143 5/23/2026
1.4.0 137 5/23/2026
1.2.0 202 5/13/2026
1.1.2 168 5/12/2026
1.1.1 261 5/3/2026
1.1.0 227 5/1/2026
1.0.3 176 4/29/2026
1.0.1 4,867 4/19/2026
1.0.0 31,463 4/18/2026