SwiftState 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SwiftState --version 2.0.0
                    
NuGet\Install-Package SwiftState -Version 2.0.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="SwiftState" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SwiftState" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SwiftState" />
                    
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 SwiftState --version 2.0.0
                    
#r "nuget: SwiftState, 2.0.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 SwiftState@2.0.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=SwiftState&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SwiftState&version=2.0.0
                    
Install as a Cake Tool

SwiftState

SwiftState is a lightweight, fluent state machine library for .NET. It allows you to define states and transitions using a clean and expressive API.

Features

  • Fluent API: Define states and transitions using a readable, chainable syntax.
  • Direct Transitions: Map inputs directly to target states.
  • Conditional Transitions: Use predicates to determine transitions based on input values.
  • Default Transitions: Specify a fallback state when no other transition matches.
  • Generic Support: Works with any type for inputs and state IDs.
  • High Performance: Uses compiled expression trees for conditional transitions and frozen dictionaries for direct lookups.

Installation

NuGet version

You can install SwiftState via NuGet Package Manager:

dotnet add package SwiftState

Or via the NuGet gallery: https://www.nuget.org/packages/SwiftState/

Usage

Basic Example

Here's a simple example of a state machine that transitions based on character inputs:

using SwiftState;

// Create a builder for a state machine with char inputs and string state IDs
var builder = new StateBuilder<char, string>("Initial");

// Define transitions
builder.When('a', "StateA");
builder.When('b', "StateB");

// Build the initial state
State<char, string> initialState = builder.Build();

// Transition
if (initialState.TryTransition('a', out State<char, string> nextState))
{
    Console.WriteLine($"Transitioned to: {nextState.Id}"); // Output: Transitioned to: StateA
}

Conditional Transitions

You can define transitions based on conditions:

var builder = new StateBuilder<int, string>("Start");

// Transition if input is greater than 10
builder.When(i => i > 10, "HighValue");

// Transition if input is even
builder.When(i => i % 2 == 0, "EvenValue");

State<int, string> state = builder.Build();

if (state.TryTransition(15, out State<int, string> nextState))
{
    Console.WriteLine(nextState.Id); // Output: HighValue
}

Default Transitions

You can specify a default state to transition to if no other conditions are met:

var builder = new StateBuilder<string, string>("Idle");

builder.When("run", "Running");
builder.Default("Error");

State<string, string> state = builder.Build();

// "jump" is not defined, so it transitions to Default
state.TryTransition("jump", out State<string, string> nextState); 
Console.WriteLine(nextState.Id); // Output: Error

Chained Transitions

You can chain transitions to define complex state flows:

var builder = new StateBuilder<char, string>("Start");

// Define a sequence: Start -> 'a' -> StateA -> 'b' -> StateB
builder.When('a', "StateA")
       .When('b', "StateB");

State<char, string> startState = builder.Build();

startState.TryTransition('a', out State<char, string> stateA);
stateA.TryTransition('b', out State<char, string> stateB);

Console.WriteLine(stateB.Id); // Output: StateB

Contributing

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

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

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
3.0.1 99 1/23/2026
3.0.0 87 1/20/2026
2.0.0 192 1/12/2026
1.2.0 127 1/10/2026
1.1.0 117 1/8/2026
1.0.2 112 1/8/2026
1.0.0 132 1/8/2026