Skip to content

About

Zero-allocation, no-boxing Result<T,E> library for .NET 9 with full CSharpFunctionalExtensions API parity.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

257 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZeroAlloc.Results

NuGet Build codecov License: MIT AOT GitHub Sponsors

Railway-style Result<T, E> for .NET 10 and 11: errors as values instead of exceptions, 0 B allocated on success and failure alike, NativeAOT-ready.

Why ZeroAlloc.Results

  • 0 B per call, on both paths. Every type is a readonly struct. Allocation tests assert 0 B for Map, Bind, Match, Tap, Ensure, Combine and LINQ query syntax, for success and failure, and for the ValueTask combinators when they complete synchronously.
  • Faster when things fail. On a hot loop where one call in three fails, it runs 4.1× faster than ErrorOr and 41× faster than FluentResults in the same benchmark run, at parity with OneOf, and allocates nothing.
  • Works under NativeAOT and trimming. No reflection. The package is marked AOT-compatible, and CI publishes and runs a NativeAOT sample with trim and AOT warnings promoted to errors.
  • A familiar API. Map, Bind, Match, Tap and Ensure, LINQ query syntax and ValueTask async variants. Coming from CSharpFunctionalExtensions, most code moves over by swapping the using.

Install

dotnet add package ZeroAlloc.Results

Quick start

using ZeroAlloc.Results;
using ZeroAlloc.Results.Extensions;

Console.WriteLine(Describe("42"));  // Welcome, age 42
Console.WriteLine(Describe("12"));  // Rejected: too young
Console.WriteLine(Describe("abc")); // Rejected: not a number

static string Describe(string input) =>
    ParseAge(input)
        .Ensure(static age => age >= 18, "too young")
        .Map(static age => $"Welcome, age {age}")
        .Match(static text => text, static error => $"Rejected: {error}");

// A value converts to success, an error converts to failure.
static Result<int, string> ParseAge(string input) =>
    int.TryParse(input, out var age) ? age : "not a number";

No exception is thrown: the first failure skips the remaining steps and lands in Match.

What you get

  • Five result shapes: pass/fail, a value or a string error, a value or a typed error, a typed error with no value, and an optional value (Maybe<T>).
  • Railway-style combinators: transform, chain, validate, inspect and unwrap a result without if checks.
  • Batch checks: combine many results into one that reports the first failure.
  • LINQ query syntax: chain dependent steps with from … in … select.
  • Async pipelines: ValueTask-based variants, plus Map and Bind that continue straight onto a ValueTask<Result<T, E>>.
  • Typed errors: use your own error structs or enums, so failures carry data rather than strings.
  • Generic failure construction: pipeline code can build a failed result for a response type it only knows as a type parameter, without reflection.

Performance

.NET 10.0.13, BenchmarkDotNet v0.15.8, all libraries in the same run. Allocations are exact; time is the ratio against ZeroAlloc.Results.

Scenario ZeroAlloc.Results OneOf ErrorOr FluentResults
Failure construct 0 B 0 B 88 B 272 B
Failure consume 0 B 0 B 0 B 240 B
Hot loop (100 iter, 1-in-3 fail), time 1.00 1.02 4.14 41.4
Hot loop, allocated 0 B 0 B 2,992 B 25,968 B

The performance page has every scenario, a comparison with CSharpFunctionalExtensions, a Result versus throw/catch benchmark and how to run them yourself.

Learn more

About

Zero-allocation, no-boxing Result<T,E> library for .NET 9 with full CSharpFunctionalExtensions API parity.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages