Interactive Examples
Every example below executes the real QuickResult package — the same assembly you'd install from NuGet — compiled to WebAssembly and running in your browser. Edit the inputs, press Run, and watch the Result change tracks.
Creating results
Success & Failure
Every operation returns a Result<T>: either a success carrying a value, or a failure carrying an IError. No exceptions, no nulls — the outcome is a value you can inspect.
var ok = Result.Success("42");
var fail = Result.Failure<string>("Something went wrong");
Console.WriteLine(ok);
Console.WriteLine(fail);
Console.WriteLine($"ok.IsSuccess: {ok.IsSuccess}");
Console.WriteLine($"fail.IsFailure: {fail.IsFailure}");
Result.Try — exceptions become failures
Result.Try runs code that might throw and converts any exception into a failure, so exception-throwing APIs plug straight into a Result flow.
var parsed = Result.Try(() => int.Parse(text));
Console.WriteLine(parsed);
Result.FromNullable — banish null checks
FromNullable converts a possibly-null value into an explicit Result: success when a value is present, a named failure when it is null. Clear the input to see the failure path.
string? name = LookupName(); // may be null
var result = Result.FromNullable(name, "No name provided");
Console.WriteLine(result);
Transforming
Match — one exit for both branches
Match collapses a Result into a single value by handling both branches in one expression — the compiler makes it impossible to forget the failure path.
var message = Result
.Try(() => int.Parse(quantity))
.Match(
onSuccess: v => $"Ordered {v} widgets",
onFailure: e => $"Could not order: {e.Message}");
Console.WriteLine(message);
Map & Bind — chain without unwrapping
Map transforms a success value; Bind chains an operation that itself returns a Result. Failures skip every later step automatically — the railway stays on the failure track.
Result<int> ParsePositive(string s) =>
int.TryParse(s, out var n) && n > 0
? Result.Success(n)
: Result.Failure<int>("Not a positive integer");
var result = Result.Success(text)
.Bind(ParsePositive)
.Map(n => n * 2);
Console.WriteLine(result);
ValueOr & the | operator — fallbacks
ValueOr unwraps a success or substitutes a fallback value. The | operator does the same at the Result level: keep the left result if it succeeded, otherwise use the right.
var parsed = Result.Try(() => int.Parse(text));
Console.WriteLine($"ValueOr(-1): {parsed.ValueOr(-1)}");
var withFallback = parsed | Result.Success(0);
Console.WriteLine($"parsed | Success(0): {withFallback}");
LINQ query syntax
LINQ query syntax
Results compose with from/select just like sequences. Each from unwraps a success; the first failure short-circuits the whole query. Try a non-number to see it stop early.
var query =
from a in Result.Try(() => int.Parse(first))
from b in Result.Try(() => int.Parse(second))
select a + b;
Console.WriteLine(query);
Mixed sync + async in one query
QuickResult's signature feature: synchronous and asynchronous steps mix freely in a single LINQ query. No Task.Result, no ceremony — await the query once at the end.
static Task<Result<int>> FetchAsync(string text) =>
Task.FromResult(Result.Try(() => int.Parse(text)));
var query =
from a in Result.Try(() => int.Parse(first)) // sync
from b in FetchAsync(second) // async
from c in Result.Try(() => int.Parse(third)) // sync again
select a + b + c;
Console.WriteLine(await query);
Guards & validation
FailIfFalse / FailIfTrue — boolean guards
Guards turn boolean checks into failures inside a query, keeping validation rules on the same railway as the rest of the flow. Change the age to cross the threshold.
var canVote =
from age in Result.Try(() => int.Parse(ageText))
from ok in Result.Success(age >= 18)
.FailIfFalse("Must be 18 or older")
select $"Age {age}: ballot issued";
Console.WriteLine(canVote);
Async pipelines
FromAsync → Try → WhenNull pipeline
A deferred pipeline wraps an async call, captures exceptions as failures, and converts null payloads into named failures — three failure modes handled in one fluent chain. Try 'throw' to simulate an exception, or clear the input to simulate null.
Task<string?> FetchAsync() =>
value == "throw"
? throw new InvalidOperationException("Simulated network failure")
: Task.FromResult<string?>(value == "" ? null : value);
var result = await Result
.FromAsync(FetchAsync)
.Try()
.WhenNull("Response was null");
Console.WriteLine(result);
Custom errors
Custom IError types
Any type implementing IError can ride in a failure, so domain errors carry structured data. Pattern match on the concrete type to recover it — no string parsing, no exception hierarchies.
public sealed record HttpResponseError(int StatusCode, string Message) : IError;
var result = Result.Failure<string>(
new HttpResponseError(statusCode, "Upstream call failed"));
var line = result.Match(
onSuccess: v => $"Fetched: {v}",
onFailure: e => e is HttpResponseError http
? $"HTTP {http.StatusCode}: {http.Message}"
: $"Unknown error: {e.Message}");
Console.WriteLine(result);
Console.WriteLine(line);
MapFailure — enrich errors on the way up
MapFailure transforms the error while leaving successes untouched — ideal for adding context (service name, correlation id) as a failure bubbles up through layers.
var result = Result.Failure<int>(message)
.MapFailure(e => new Error($"[orders-service] {e.Message}"));
Console.WriteLine(result);
C# unions
Switch on a Result as a C# union
On .NET 10 and later, Result<T> is a C# union of T and IError. With C# 15 you can switch on the result itself by type — the compiler checks that both cases are handled — and a value or an error converts implicitly to a result, so a method can return either directly.
public sealed record OutOfStockError(string Message) : IError;
Result<int> Reserve(string text)
{
if (!int.TryParse(text, out var requested) || requested <= 0)
return new Error("Quantity must be a positive whole number");
if (requested > 10)
return new OutOfStockError($"Only 10 in stock, {requested} requested");
return requested;
}
var result = Reserve(quantity);
var line = result switch
{
int reserved => $"Reserved {reserved} widgets",
OutOfStockError e => $"409 Conflict: {e.Message}",
IError e => $"400 Bad Request: {e.Message}",
};
Console.WriteLine(result);
Console.WriteLine(line);