ZeroAlloc.Results
0.1.2
dotnet add package ZeroAlloc.Results --version 0.1.2
NuGet\Install-Package ZeroAlloc.Results -Version 0.1.2
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.Results" Version="0.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZeroAlloc.Results" Version="0.1.2" />
<PackageReference Include="ZeroAlloc.Results" />
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.Results --version 0.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ZeroAlloc.Results, 0.1.2"
#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.Results@0.1.2
#: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.Results&version=0.1.2
#tool nuget:?package=ZeroAlloc.Results&version=0.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ZeroAlloc.Results
ZeroAlloc.Results is a zero-allocation, no-boxing Result<T, E> library for .NET 9. All types are readonly struct — no heap allocation, no boxing, no GC pressure.
Install
dotnet add package ZeroAlloc.Results
Quick Example
using ZeroAlloc.Results;
using ZeroAlloc.Results.Extensions;
// Create
Result<int, string> ok = Result<int, string>.Success(42);
Result<int, string> fail = Result<int, string>.Failure("not found");
// Or via implicit conversion
Result<int, string> r = 42; // success
Result<int, string> e = "error"; // failure
// Chain with Map, Bind, Ensure
var result = GetUser(id)
.Ensure(u => u.IsActive, "user is inactive")
.Map(u => u.Email)
.Bind(email => SendWelcome(email));
// Match to extract
string message = result.Match(
onSuccess: email => $"Sent to {email}",
onFailure: err => $"Failed: {err}");
// LINQ query syntax
var greeting =
from user in GetUser(id)
from profile in GetProfile(user)
select $"Hello, {profile.Name}";
// Async pipelines with ValueTask
var response = await GetUser(id)
.MapAsync(async u => await LoadPermissions(u))
.BindAsync(async p => await BuildToken(p));
Types
| Type | Success | Error | Use case |
|---|---|---|---|
Result |
— | string |
simple pass/fail |
Result<T> |
T |
string |
most common |
Result<T, E> |
T |
E |
fully generic |
UnitResult<E> |
— | E |
typed error, no value |
Maybe<T> |
T |
— | optional value |
API
| Method | Description |
|---|---|
Map(T→U) |
Transform the success value |
MapError(E→F) |
Transform the error value |
Bind(T→Result<U,E>) |
Chain result-returning functions |
Match(onSuccess, onFailure) |
Extract a value from either branch |
Tap(T→void) |
Side-effect on success, pass through |
TapError(E→void) |
Side-effect on failure, pass through |
Ensure(T→bool, E) |
Validate success value |
Combine(Span<Result<T,E>>) |
Merge multiple results, zero-alloc |
*Async |
ValueTask-based variant of each combinator |
Performance
All core operations produce zero heap allocations (Windows 11, .NET 9.0.14, BenchmarkDotNet v0.13.12).
| Method | Mean | Allocated |
|---|---|---|
Result<int,string>.Success(42) |
0.25 ns | 0 B |
Result<int,string>.Failure("err") |
0.43 ns | 0 B |
.Map(x => x * 2) |
2.92 ns | 0 B |
.Bind(x => Success(x.ToString())) |
8.81 ns | 0 B |
.Match(v => v, _ => -1) |
2.02 ns | 0 B |
Maybe<int>.Some(42) |
3.66 ns | 0 B |
UnitResult<string>.Success() |
0.35 ns | 0 B |
Head-to-head vs CSharpFunctionalExtensions 3.7.0 (Windows 11, .NET 9.0.14, BenchmarkDotNet v0.13.12):
| Category | ZeroAlloc.Results | CSharpFunctionalExtensions | Allocated | Ratio |
|---|---|---|---|---|
Create_Success |
0.33 ns | 2.89 ns | 0 B both | 8.7× faster |
Create_Failure |
0.30 ns | 1.44 ns | 0 B both | 4.8× faster |
Map |
1.09 ns | 1.48 ns | 0 B both | 1.4× faster |
Match |
0.37 ns | 0.68 ns | 0 B both | 1.9× faster |
Chain (Map+Bind+Match) |
2.28 ns | 2.45 ns | 0 B both | 1.1× faster |
See docs/performance.md for the full benchmark analysis and zero-allocation design explanation.
Documentation
| Page | Description |
|---|---|
| Getting Started | Install and write your first result pipeline |
| Types | All five result types and when to use each |
| Combinators | Map, Bind, Match, Tap, Ensure, Combine |
| Async | ValueTask async variants for all combinators |
| LINQ | Query syntax with Select and SelectMany |
| Performance | Zero-alloc design and benchmark results |
License
MIT
| Product | Versions 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.