See More

A very common reason is a wrong site baseUrl configuration.\n

Current configured baseUrl = / (default value)\n

We suggest trying baseUrl = \n\n',document.body.prepend(n);var e=document.getElementById("__docusaurus-base-url-issue-banner-suggestion-container"),s=window.location.pathname,o="/"===s.substr(-1)?s:s+"/";e.textContent=o}document.addEventListener("DOMContentLoaded",function(){void 0===window.docusaurus&&insertBanner()})

Skip to main content

ZeroAlloc.StateMachine

Source-generated, zero-allocation finite state machines for .NET.

Add [StateMachine] and [Transition<TState, TTrigger>] to a partial class or struct. The generator emits a TryFire(TTrigger) method as a switch expression over (TState, TTrigger) tuples — no dictionary, no delegate dispatch, no heap allocation on the transition path.


Quick Example

[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 { }

var machine = new OrderMachine();
machine.TryFire(Trigger.Submit); // true — Idle → Pending
machine.Current; // Pending

Contents

PageDescription
Getting StartedInstall and define your first machine
Attributes[StateMachine], [Transition], [Terminal] reference
Source GeneratorWhat the generator emits — input/output examples
TestingUnit-test state machines without mocking
AOT & TrimmingNative AOT compatibility
PerformanceBenchmark results and allocation profile

Core Concepts

PageDescription
States and TriggersEnums as states and triggers, naming conventions
TransitionsDirected edges, TryFire, ordering, entry/exit contract
Concurrent ModeCAS loop, Volatile.Read, hook ordering, guard restrictions

Guides

PageDescription
GuardsBlock transitions at runtime with When = true
Entry and Exit ActionsReact to state crossings with partial void hooks
Terminal StatesIntentional sinks and the [Terminal] attribute
Circuit Breaker ExampleReal-world use: thread-safe circuit breaker

Diagnostics

IDSeverityDescription
ZSM0001WarningUnreachable state
ZSM0002WarningUnintentional sink state
ZSM0003WarningSingle-use trigger (possible typo)
ZSM0004ErrorConcurrent mode on a struct