ResultZero 2.0.0

dotnet add package ResultZero --version 2.0.0                
NuGet\Install-Package ResultZero -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="ResultZero" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ResultZero --version 2.0.0                
#r "nuget: ResultZero, 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.
// Install ResultZero as a Cake Addin
#addin nuget:?package=ResultZero&version=2.0.0

// Install ResultZero as a Cake Tool
#tool nuget:?package=ResultZero&version=2.0.0                

ResultZero

NuGet

Zero-allocation result pattern for C#.

Normally, exceptions are a great choice for error handling in C#. However, they can be notoriously slow when used for control flow. The result pattern can be better for:

  • Parsers that are expected to handle erroneous input
  • Validating user input in server APIs

ResultZero is designed with the following goals in mind:

  • Zero memory allocation for both success and error cases
  • No redundant features or bloat
  • Scalable for all use cases

Example

static Result<int> Divide(int Numerator, int Denominator) {
    if (Denominator == 0) {
        return new Error("Cannot divide by zero.");
    }
    return Numerator / Denominator;
}

// Unwrap results, throwing if error
Console.WriteLine(Divide(10, 2).Value);

// Check if results errored
if (Divide(10, 0).IsError) {
    Console.WriteLine("Denominator was 0");
}

// Advanced error handling
if (Divide(3, 1).TryGetValue(out int Value, out Error Error)) {
    Console.WriteLine(Value);
}
else {
    Error.Throw();
}

Benchmarks

Comparison between ResultZero, FluentResults and exceptions:

Method Mean Error StdDev Median Gen0 Allocated
SuccessResultZero 1.9420 ns 0.0118 ns 0.0111 ns 1.9411 ns - -
SuccessFluentResults 56.0877 ns 0.4140 ns 0.3457 ns 56.0095 ns 0.0510 160 B
SuccessExceptions 0.0015 ns 0.0029 ns 0.0028 ns 0.0000 ns - -
FailureResultZero 0.0020 ns 0.0039 ns 0.0036 ns 0.0000 ns - -
FailureFluentResults 206.8702 ns 1.0955 ns 0.9148 ns 206.9100 ns 0.1938 608 B
FailureExceptions 2,622.5916 ns 12.3147 ns 10.9167 ns 2,623.9363 ns 0.0992 320 B
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ResultZero:

Package Downloads
HjsonSharp

A customisable streaming parser for HJSON, JSON, JSONC and JSON5.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 6 1/24/2025
1.0.0 47 1/19/2025