Meziantou.Framework.UndoRedo 2.0.0

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

Meziantou.Framework.UndoRedo

Meziantou.Framework.UndoRedo provides an async-first undo/redo framework based on the command pattern. Every action exposes asynchronous ExecuteAsync / UnExecuteAsync methods (returning ValueTask), supports cancellation, and can be grouped into transactions or merged into a single undo step.

Record, undo and redo an action

The simplest way to record an action is to provide the execute and unexecute delegates:

var manager = new UndoRedoManager();
var list = new List<int>();

await manager.RecordActionAsync(
    execute: () => list.Add(1),
    unexecute: () => list.RemoveAt(list.Count - 1));

// list = [1]
await manager.UndoAsync(); // list = []
await manager.RedoAsync(); // list = [1]

CanUndo and CanRedo indicate whether an operation is available, and CollectionChanged is raised whenever the history changes.

The execute and unexecute delegates can each be synchronous or asynchronous, and the two may be mixed. Build the action with UndoRedoDelegateAction and record it:

// Asynchronous execute, synchronous unexecute
var action = new UndoRedoDelegateAction(
    execute: ct => SaveAsync(ct),
    unexecute: () => Restore());

await manager.RecordActionAsync(action);

Custom action

For reusable actions, derive from UndoRedoActionBase. The base class guards against executing or reverting twice in a row.

sealed class AddItemAction(IList<int> list, int value) : UndoRedoActionBase
{
    protected override ValueTask ExecuteCoreAsync(CancellationToken cancellationToken)
    {
        list.Add(value);
        return ValueTask.CompletedTask;
    }

    protected override ValueTask UnExecuteCoreAsync(CancellationToken cancellationToken)
    {
        list.Remove(value);
        return ValueTask.CompletedTask;
    }
}

await manager.RecordActionAsync(new AddItemAction(list, 42));

Group actions in a transaction

A transaction groups several actions into a single undo step. Disposing the transaction commits it (use await using), or call RollbackAsync to revert it.

await using (manager.CreateTransaction())
{
    await manager.RecordActionAsync(addFirst, removeFirst);
    await manager.RecordActionAsync(addSecond, removeSecond);
}

// A single UndoAsync reverts both actions
await manager.UndoAsync();

Merge consecutive actions

When an action sets AllowToMergeWithPrevious and the previous action's TryToMergeAsync returns true, both collapse into a single undo step. This is useful for chains of similar operations such as typing or dragging.

Note: UndoRedoManager is not thread-safe; operate on it from a single logical flow.

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

    • No dependencies.
  • net11.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.0.0 116 7/5/2026
1.0.1 113 6/13/2026
1.0.0 107 6/2/2026