Skip to content

OnixLabs.Core.Result

Matthew Layton edited this page Nov 15, 2026 · 4 revisions

Result Class

Represents a non-generic result value that signifies either the successful completion of an operation or the exception thrown by a failed operation — the foundation of railway-oriented, exception-free error handling.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → Result
Derived Success, Failure
Implements IValueEquatable<Result>

Definition

public closed class Result : IValueEquatable<Result>

Result is a C# 15 closed class with exactly two sealed derivations, Success and Failure. It cannot be instantiated directly, and its constructor is private protected, so no other assembly can derive from it; the only way to obtain a Result is through the static factory members described below or the implicit conversion from Exception.

Because the hierarchy is closed, the compiler knows that Success and Failure are the only cases. With C# 15, a switch over them is exhaustive without a discard arm:

string message = result switch
{
    Success => "Done.",
    Failure failure => $"Error: {failure.Exception.Message}"
};

This works on every target framework, including .NET 8.0. Consumers compiling with an earlier language version see no change and can keep their existing discard arms.

For results that carry a value on success, see Result<T>.

Remarks

Result models the outcome of an operation that either succeeds (carrying no value) or fails (carrying an Exception). It is the void-returning counterpart of Result<T> and is intended to replace the try/catch/throw control-flow pattern with explicit, composable values.

This is the railway-oriented programming pattern. An operation is modelled as a track with two rails: a success rail and a failure rail. Combinators such as Select and SelectMany only execute their delegate when the current Result is on the success rail; once a Result becomes a Failure, it short-circuits — every subsequent combinator passes the original failure through untouched, so the captured exception is never lost and never re-evaluated.

Key behaviours and gotchas:

  • Capture, don't throw. The factory methods Of and OfAsync invoke a delegate, returning Success if it completes or wrapping any thrown Exception in a Failure. The exception is captured as data rather than propagated.
  • Implicit conversion from Exception. Any Exception converts implicitly to a Failure, which is why combinator catch blocks can simply return exception;.
  • Deferred re-throw. Throw and GetExceptionOrThrow let you leave the rails. Throw rethrows the captured exception while preserving its original stack trace via ExceptionDispatchInfo.
  • Value equality. Two Success instances are always equal; two Failure instances are equal when their Exception references are equal. Equals, GetHashCode, and ToString are sealed.
  • ToString returns the failure message. For a Success it returns string.Empty; for a Failure it returns Exception.Message.

Match, Select, and SelectMany each have asynchronous overloads (MatchAsync, SelectAsync, SelectManyAsync) declared directly on Result. Each accepts Task-returning or ValueTask-returning delegates and a trailing CancellationToken cancellationToken = default; overloads taking ValueTask-returning delegates return ValueTask/ValueTask<TResult>. Further overloads over Task<Result> and ValueTask<Result> receivers are provided as extension methods; see ResultExtensions.

Overload resolution note MatchAsync, SelectAsync, and SelectManyAsync have both Task-returning and ValueTask-returning delegate overloads. An async lambda (or a lambda whose body is only a throw expression) is convertible to both delegate types, so such a call is ambiguous and fails with compiler error CS0121. Give the lambda an explicit return type to select an overload, for example async Task<Result> (_) => ... or async ValueTask (value) => ....

The two concrete derivations are documented separately:

  • Success — the successful case; carries no value.
  • Failure — the failed case; carries the captured Exception.

Examples

Capturing an operation as a Result

using OnixLabs.Core;

Result result = Result.Of(() => File.Delete("/tmp/scratch.txt"));

result.Match(
    success: () => Console.WriteLine("Deleted."),
    failure: exception => Console.WriteLine($"Failed: {exception.Message}"));

Composing operations on the success rail

Result pipeline = Result
    .Of(() => ValidateRequest(request))
    .SelectMany(() => Result.Of(() => PersistRequest(request)))
    .Select(() => Audit(request));

// If ValidateRequest throws, PersistRequest and Audit never run;
// the original exception flows straight through to the end.
if (pipeline.IsFailure)
    logger.LogError(pipeline.GetExceptionOrThrow(), "Pipeline failed.");

Leaving the rails

Result result = Result.Of(DoWork);
result.Throw(); // Rethrows the captured exception (with original stack trace) if failed; no-op if successful.

Static Methods

Method Returns Description
Of(Action) Result Invokes an action, returning Success or a Failure wrapping any thrown exception.
OfAsync(Func<Task>, CancellationToken) Task<Result> Asynchronously invokes a function, returning Success or a Failure wrapping any thrown exception.
OfAsync(Func<CancellationToken, Task>, CancellationToken) Task<Result> Asynchronously invokes a cancellable function, returning Success or a Failure wrapping any thrown exception.
Success() Success Creates a successful result.
Failure(Exception) Failure Creates a failed result wrapping the specified exception.

Of(Action)

public static Result Of(Action action)

Invokes the specified action. If the action completes without throwing, a Success is returned; otherwise the thrown Exception is captured and returned as a Failure.

Parameter Type Description
action Action The action to invoke to obtain a successful or failed result.

Returns — A Success if action completes; otherwise a Failure wrapping the thrown exception.

OfAsync(Func<Task>, CancellationToken)

public static async Task<Result> OfAsync(Func<Task> func, CancellationToken cancellationToken = default)

Asynchronously invokes the specified function. If it completes without throwing, a Success is returned; otherwise the thrown Exception is captured and returned as a Failure.

Parameter Type Description
func Func<Task> The function to invoke to obtain a successful or failed result.
cancellationToken CancellationToken The token to monitor for cancellation requests.

Returns — A Task<Result> yielding a Success if func completes; otherwise a Failure wrapping the thrown exception.

OfAsync(Func<CancellationToken, Task>, CancellationToken)

public static async Task<Result> OfAsync(Func<CancellationToken, Task> func, CancellationToken cancellationToken = default)

Asynchronously invokes the specified cancellable function, passing cancellationToken to it. If it completes without throwing, a Success is returned; otherwise the thrown Exception is captured and returned as a Failure.

Parameter Type Description
func Func<CancellationToken, Task> The function to invoke to obtain a successful or failed result.
cancellationToken CancellationToken The token to monitor for cancellation requests, which is passed to the invoked function.

Returns — A Task<Result> yielding a Success if func completes; otherwise a Failure wrapping the thrown exception.

Success()

public static Success Success()

Creates a result representing a successful outcome. All calls return the same singleton instance.

Returns — The singleton Success instance.

Failure(Exception)

public static Failure Failure(Exception exception)

Creates a result representing a failed outcome wrapping the specified exception.

Parameter Type Description
exception Exception The underlying failed exception value.

Returns — A new Failure wrapping exception.

Properties

Property Type Description
IsSuccess bool Gets a value indicating whether the result is in a successful state.
IsFailure bool Gets a value indicating whether the result is in a failed state.

IsSuccess

public bool IsSuccess { get; }

Gets a value indicating whether the current Result is in a successful state (that is, whether it is a Success).

IsFailure

public bool IsFailure { get; }

Gets a value indicating whether the current Result is in a failed state (that is, whether it is a Failure).

Instance Methods

Method Returns Description
GetExceptionOrDefault() Exception? Gets the underlying exception, or null if successful.
GetExceptionOrDefault(Exception) Exception Gets the underlying exception, or a specified default if successful.
GetExceptionOrNone() Optional<Exception> Gets the underlying exception as an Optional<Exception>.
GetExceptionOrThrow() Exception Gets the underlying exception, or throws if successful.
Match(Action, Action<Exception>) void Executes the action that matches the current state.
Match<TResult>(Func<TResult>, Func<Exception, TResult>) TResult Executes the matching delegate and returns its result.
MatchAsync(...) Task / Task<TResult> / ValueTask / ValueTask<TResult> Asynchronously executes the matching delegate.
Select(Action) Result Applies a selector if successful, returning a Result.
Select<TResult>(Func<TResult>) Result<TResult> Applies a value-producing selector if successful, returning a Result<TResult>.
SelectAsync(...) Task<Result> / Task<Result<TResult>> / ValueTask<Result> / ValueTask<Result<TResult>> Asynchronously applies a selector if successful.
SelectMany(Func<Result>) Result Applies a Result-producing selector if successful.
SelectMany<TResult>(Func<Result<TResult>>) Result<TResult> Applies a Result<TResult>-producing selector if successful.
SelectManyAsync(...) Task<Result> / Task<Result<TResult>> / ValueTask<Result> / ValueTask<Result<TResult>> Asynchronously applies a Result-producing selector if successful.
Throw() void Rethrows the captured exception if failed; otherwise a no-op.
Equals(Result?) bool Determines whether this result equals another.
Equals(object?) bool Determines whether this result equals another object.
GetHashCode() int Returns a hash code for this result.
ToString() string Returns the failure message, or an empty string if successful.

GetExceptionOrDefault()

public Exception? GetExceptionOrDefault()

Gets the underlying exception if the current Result is a Failure; otherwise null.

Returns — The captured Exception, or null when successful.

GetExceptionOrDefault(Exception)

public Exception GetExceptionOrDefault(Exception defaultException)

Gets the underlying exception if the current Result is a Failure; otherwise returns defaultException.

Parameter Type Description
defaultException Exception The default exception to return when the result is successful.

Returns — The captured Exception, or defaultException when successful.

GetExceptionOrNone()

public Optional<Exception> GetExceptionOrNone()

Gets the underlying exception as an Optional<Exception>. Returns a Some wrapping the captured exception when failed, or None when successful.

Returns — An Optional<Exception> containing the captured exception, or None.

GetExceptionOrThrow()

public Exception GetExceptionOrThrow()

Gets the underlying exception if the current Result is a Failure; otherwise throws.

Returns — The captured Exception.

Exceptions

Exception Condition
InvalidOperationException The current result is in a success state, so there is no exception to retrieve.

Match(Action, Action<Exception>)

public void Match(Action? success = null, Action<Exception>? failure = null)

Executes the delegate that matches the current state. If the result is a Failure, failure is invoked with the captured exception; otherwise success is invoked. Both delegates are optional; a null delegate for the matching branch is simply not invoked.

Parameter Type Description
success Action? The delegate to execute when the result is successful.
failure Action<Exception>? The delegate to execute when the result is failed.

Match<TResult>(Func<TResult>, Func<Exception, TResult>)

public TResult Match<TResult>(Func<TResult> success, Func<Exception, TResult> failure)

Executes the delegate that matches the current state and returns its result.

Parameter Type Description
success Func<TResult> The delegate to execute when the result is successful.
failure Func<Exception, TResult> The delegate to execute when the result is failed.

Returns — The result of success when successful; otherwise the result of failure.

MatchAsync Overloads

Asynchronously executes the delegate that matches the state of the current Result. Overloads cover combinations of synchronous, Task-returning, and ValueTask-returning success/failure delegates (at least one asynchronous), with and without a CancellationToken parameter, for both the void and value-returning (<TResult>) forms. Both delegates are optional in the void forms.

Overload Returns Delegates
MatchAsync(Func<Task>?, Action<Exception>?, CancellationToken) Task success: Task-returning; failure: synchronous
MatchAsync(Func<ValueTask>?, Action<Exception>?, CancellationToken) ValueTask success: ValueTask-returning; failure: synchronous
MatchAsync(Func<CancellationToken, Task>?, Action<Exception>?, CancellationToken) Task success: Task-returning (cancellable); failure: synchronous
MatchAsync(Func<CancellationToken, ValueTask>?, Action<Exception>?, CancellationToken) ValueTask success: ValueTask-returning (cancellable); failure: synchronous
MatchAsync(Action?, Func<Exception, Task>?, CancellationToken) Task success: synchronous; failure: Task-returning
MatchAsync(Action?, Func<Exception, ValueTask>?, CancellationToken) ValueTask success: synchronous; failure: ValueTask-returning
MatchAsync(Action?, Func<Exception, CancellationToken, Task>?, CancellationToken) Task success: synchronous; failure: Task-returning (cancellable)
MatchAsync(Action?, Func<Exception, CancellationToken, ValueTask>?, CancellationToken) ValueTask success: synchronous; failure: ValueTask-returning (cancellable)
MatchAsync(Func<Task>?, Func<Exception, Task>?, CancellationToken) Task success: Task-returning; failure: Task-returning
MatchAsync(Func<ValueTask>?, Func<Exception, ValueTask>?, CancellationToken) ValueTask success: ValueTask-returning; failure: ValueTask-returning
MatchAsync(Func<CancellationToken, Task>?, Func<Exception, CancellationToken, Task>?, CancellationToken) Task success: Task-returning (cancellable); failure: Task-returning (cancellable)
MatchAsync(Func<CancellationToken, ValueTask>?, Func<Exception, CancellationToken, ValueTask>?, CancellationToken) ValueTask success: ValueTask-returning (cancellable); failure: ValueTask-returning (cancellable)
MatchAsync<TResult>(Func<Task<TResult>>, Func<Exception, TResult>, CancellationToken) Task<TResult> success: Task-returning; failure: synchronous
MatchAsync<TResult>(Func<ValueTask<TResult>>, Func<Exception, TResult>, CancellationToken) ValueTask<TResult> success: ValueTask-returning; failure: synchronous
MatchAsync<TResult>(Func<CancellationToken, Task<TResult>>, Func<Exception, TResult>, CancellationToken) Task<TResult> success: Task-returning (cancellable); failure: synchronous
MatchAsync<TResult>(Func<CancellationToken, ValueTask<TResult>>, Func<Exception, TResult>, CancellationToken) ValueTask<TResult> success: ValueTask-returning (cancellable); failure: synchronous
MatchAsync<TResult>(Func<TResult>, Func<Exception, Task<TResult>>, CancellationToken) Task<TResult> success: synchronous; failure: Task-returning
MatchAsync<TResult>(Func<TResult>, Func<Exception, ValueTask<TResult>>, CancellationToken) ValueTask<TResult> success: synchronous; failure: ValueTask-returning
MatchAsync<TResult>(Func<TResult>, Func<Exception, CancellationToken, Task<TResult>>, CancellationToken) Task<TResult> success: synchronous; failure: Task-returning (cancellable)
MatchAsync<TResult>(Func<TResult>, Func<Exception, CancellationToken, ValueTask<TResult>>, CancellationToken) ValueTask<TResult> success: synchronous; failure: ValueTask-returning (cancellable)
MatchAsync<TResult>(Func<Task<TResult>>, Func<Exception, Task<TResult>>, CancellationToken) Task<TResult> success: Task-returning; failure: Task-returning
MatchAsync<TResult>(Func<ValueTask<TResult>>, Func<Exception, ValueTask<TResult>>, CancellationToken) ValueTask<TResult> success: ValueTask-returning; failure: ValueTask-returning
MatchAsync<TResult>(Func<CancellationToken, Task<TResult>>, Func<Exception, CancellationToken, Task<TResult>>, CancellationToken) Task<TResult> success: Task-returning (cancellable); failure: Task-returning (cancellable)
MatchAsync<TResult>(Func<CancellationToken, ValueTask<TResult>>, Func<Exception, CancellationToken, ValueTask<TResult>>, CancellationToken) ValueTask<TResult> success: ValueTask-returning (cancellable); failure: ValueTask-returning (cancellable)
public async Task MatchAsync(Func<Task>? success = null, Action<Exception>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<ValueTask>? success = null, Action<Exception>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Func<CancellationToken, Task>? success = null, Action<Exception>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<CancellationToken, ValueTask>? success = null, Action<Exception>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Action? success = null, Func<Exception, Task>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Action? success = null, Func<Exception, ValueTask>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Action? success = null, Func<Exception, CancellationToken, Task>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Action? success = null, Func<Exception, CancellationToken, ValueTask>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Func<Task>? success = null, Func<Exception, Task>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<ValueTask>? success = null, Func<Exception, ValueTask>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Func<CancellationToken, Task>? success = null, Func<Exception, CancellationToken, Task>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<CancellationToken, ValueTask>? success = null, Func<Exception, CancellationToken, ValueTask>? failure = null, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<Task<TResult>> success, Func<Exception, TResult> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<ValueTask<TResult>> success, Func<Exception, TResult> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<CancellationToken, Task<TResult>> success, Func<Exception, TResult> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<CancellationToken, ValueTask<TResult>> success, Func<Exception, TResult> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<TResult> success, Func<Exception, Task<TResult>> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<TResult> success, Func<Exception, ValueTask<TResult>> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<TResult> success, Func<Exception, CancellationToken, Task<TResult>> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<TResult> success, Func<Exception, CancellationToken, ValueTask<TResult>> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<Task<TResult>> success, Func<Exception, Task<TResult>> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<ValueTask<TResult>> success, Func<Exception, ValueTask<TResult>> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<CancellationToken, Task<TResult>> success, Func<Exception, CancellationToken, Task<TResult>> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<CancellationToken, ValueTask<TResult>> success, Func<Exception, CancellationToken, ValueTask<TResult>> failure, CancellationToken cancellationToken = default)

Parameters

Parameter Description
success The delegate to execute when the current Result instance is in a successful state.
failure The delegate to execute when the current Result instance is in a failed state.
cancellationToken The token to monitor for cancellation requests.

Type Parameters

Type Parameter Description
TResult The underlying type of the result produced by the matching delegate.

Returns

  • Task / ValueTask — A Task or ValueTask (matching the overload) that represents the asynchronous operation.
  • Task<TResult> / ValueTask<TResult> — The result of the success delegate if the current Result instance is in a successful state; otherwise, returns the result of the failure delegate if the current Result instance is in a failed state.

Every overload that takes a Task-returning delegate has a twin that takes the equivalent ValueTask-returning delegate and returns ValueTask or ValueTask<…> instead of Task or Task<…>. See the overload resolution note under Remarks.

Select(Action)

public Result Select(Action selector)

If the current Result is a Failure, it is returned unchanged. Otherwise selector is invoked: a Success is returned if it completes, or a Failure wrapping any thrown exception.

Parameter Type Description
selector Action The delegate to apply to the current Result.

Returns — A Success if currently successful and selector completes; otherwise a Failure.

Select<TResult>(Func<TResult>)

public Result<TResult> Select<TResult>(Func<TResult> selector)

If the current Result is a Failure, a Failure<TResult> carrying the same exception is returned. Otherwise selector is invoked: its value becomes a Success<TResult>, or any thrown exception becomes a Failure<TResult>.

Parameter Type Description
selector Func<TResult> The value-producing delegate to apply to the current Result.

Returns — A Result<TResult> representing the outcome.

SelectAsync Overloads

Asynchronously applies the selector delegate if the current Result is successful; otherwise the failure is propagated. Overloads accept Task-returning or ValueTask-returning selectors, with and without a CancellationToken parameter, producing either a Result or a Result<TResult>. Any exception thrown by the selector is captured as a failure.

Overload Returns Delegates
SelectAsync(Func<Task>, CancellationToken) Task<Result> selector: Task-returning
SelectAsync(Func<ValueTask>, CancellationToken) ValueTask<Result> selector: ValueTask-returning
SelectAsync(Func<CancellationToken, Task>, CancellationToken) Task<Result> selector: Task-returning (cancellable)
SelectAsync(Func<CancellationToken, ValueTask>, CancellationToken) ValueTask<Result> selector: ValueTask-returning (cancellable)
SelectAsync<TResult>(Func<Task<TResult>>, CancellationToken) Task<Result<TResult>> selector: Task-returning
SelectAsync<TResult>(Func<ValueTask<TResult>>, CancellationToken) ValueTask<Result<TResult>> selector: ValueTask-returning
SelectAsync<TResult>(Func<CancellationToken, Task<TResult>>, CancellationToken) Task<Result<TResult>> selector: Task-returning (cancellable)
SelectAsync<TResult>(Func<CancellationToken, ValueTask<TResult>>, CancellationToken) ValueTask<Result<TResult>> selector: ValueTask-returning (cancellable)
public async Task<Result> SelectAsync(Func<Task> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result> SelectAsync(Func<ValueTask> selector, CancellationToken cancellationToken = default)
public async Task<Result> SelectAsync(Func<CancellationToken, Task> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result> SelectAsync(Func<CancellationToken, ValueTask> selector, CancellationToken cancellationToken = default)
public async Task<Result<TResult>> SelectAsync<TResult>(Func<Task<TResult>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result<TResult>> SelectAsync<TResult>(Func<ValueTask<TResult>> selector, CancellationToken cancellationToken = default)
public async Task<Result<TResult>> SelectAsync<TResult>(Func<CancellationToken, Task<TResult>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result<TResult>> SelectAsync<TResult>(Func<CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken = default)

Parameters

Parameter Description
selector The delegate to apply to current Result instance. The delegate to apply to the current Result instance.
cancellationToken The token to monitor for cancellation requests.

Type Parameters

Type Parameter Description
TResult The underlying type of the result produced by the selector delegate.

Returns

  • Task<Result> / ValueTask<Result> — Success if the current Result is in a successful state, and the delegate invocation is also successful; otherwise; Failure.
  • Task<Result<TResult>> / ValueTask<Result<TResult>> — Success<T> if the current Result is in a successful state, and the delegate invocation is also successful; otherwise; Failure<T>.

Every overload that takes a Task-returning delegate has a twin that takes the equivalent ValueTask-returning delegate and returns ValueTask or ValueTask<…> instead of Task or Task<…>. See the overload resolution note under Remarks.

SelectMany(Func<Result>)

public Result SelectMany(Func<Result> selector)

If the current Result is a Failure, it is returned unchanged. Otherwise selector is invoked and its Result is returned; any thrown exception is captured as a Failure. Use this to chain operations that themselves return a Result, flattening the outcome rather than nesting it.

Parameter Type Description
selector Func<Result> The Result-producing delegate to apply to the current Result.

Returns — The Result produced by selector, the original failure, or a Failure wrapping a thrown exception.

SelectMany<TResult>(Func<Result<TResult>>)

public Result<TResult> SelectMany<TResult>(Func<Result<TResult>> selector)

If the current Result is a Failure, a Failure<TResult> carrying the same exception is returned. Otherwise selector is invoked and its Result<TResult> is returned; any thrown exception is captured as a Failure<TResult>.

Parameter Type Description
selector Func<Result<TResult>> The Result<TResult>-producing delegate to apply to the current Result.

Returns — A Result<TResult> representing the outcome.

SelectManyAsync Overloads

Asynchronously applies a Result-producing selector if the current Result is successful, flattening the outcome; otherwise the failure is propagated. Overloads accept Task-returning or ValueTask-returning selectors, with and without a CancellationToken parameter. Any exception thrown by the selector is captured as a failure.

Overload Returns Delegates
SelectManyAsync(Func<Task<Result>>, CancellationToken) Task<Result> selector: Task-returning
SelectManyAsync(Func<ValueTask<Result>>, CancellationToken) ValueTask<Result> selector: ValueTask-returning
SelectManyAsync(Func<CancellationToken, Task<Result>>, CancellationToken) Task<Result> selector: Task-returning (cancellable)
SelectManyAsync(Func<CancellationToken, ValueTask<Result>>, CancellationToken) ValueTask<Result> selector: ValueTask-returning (cancellable)
SelectManyAsync<TResult>(Func<Task<Result<TResult>>>, CancellationToken) Task<Result<TResult>> selector: Task-returning
SelectManyAsync<TResult>(Func<ValueTask<Result<TResult>>>, CancellationToken) ValueTask<Result<TResult>> selector: ValueTask-returning
SelectManyAsync<TResult>(Func<CancellationToken, Task<Result<TResult>>>, CancellationToken) Task<Result<TResult>> selector: Task-returning (cancellable)
SelectManyAsync<TResult>(Func<CancellationToken, ValueTask<Result<TResult>>>, CancellationToken) ValueTask<Result<TResult>> selector: ValueTask-returning (cancellable)
public async Task<Result> SelectManyAsync(Func<Task<Result>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result> SelectManyAsync(Func<ValueTask<Result>> selector, CancellationToken cancellationToken = default)
public async Task<Result> SelectManyAsync(Func<CancellationToken, Task<Result>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result> SelectManyAsync(Func<CancellationToken, ValueTask<Result>> selector, CancellationToken cancellationToken = default)
public async Task<Result<TResult>> SelectManyAsync<TResult>(Func<Task<Result<TResult>>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result<TResult>> SelectManyAsync<TResult>(Func<ValueTask<Result<TResult>>> selector, CancellationToken cancellationToken = default)
public async Task<Result<TResult>> SelectManyAsync<TResult>(Func<CancellationToken, Task<Result<TResult>>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result<TResult>> SelectManyAsync<TResult>(Func<CancellationToken, ValueTask<Result<TResult>>> selector, CancellationToken cancellationToken = default)

Parameters

Parameter Description
selector The delegate to apply to the current Result instance.
cancellationToken The token to monitor for cancellation requests.

Type Parameters

Type Parameter Description
TResult The underlying type of the result produced by the selector delegate.

Returns

  • Task<Result> / ValueTask<Result> — Success if the current Result is in a successful state, and the delegate invocation is also successful; otherwise; Failure.
  • Task<Result<TResult>> / ValueTask<Result<TResult>> — Success<T> if the current Result is in a successful state, and the delegate invocation is also successful; otherwise; Failure<T>.

Every overload that takes a Task-returning delegate has a twin that takes the equivalent ValueTask-returning delegate and returns ValueTask or ValueTask<…> instead of Task or Task<…>. See the overload resolution note under Remarks.

Throw()

public void Throw()

If the current Result is a Failure, rethrows the captured exception; otherwise does nothing. The original stack trace is preserved via ExceptionDispatchInfo, and the rethrow point is recorded as a continuation.

Exceptions

Exception Condition
The captured Exception The current result is in a failed state.

Equals(Result?)

public bool Equals(Result? other)

Determines whether the current result equals another. Two Success instances are always equal; two Failure instances are equal when their Exception references are equal.

Returns — true if the results are equal; otherwise, false.

Equals(object?)

public sealed override bool Equals(object? obj)

Determines whether the current result equals another object. Behaves as Equals(Result?) when obj is a Result.

Returns — true if obj is an equal Result; otherwise, false.

GetHashCode()

public sealed override int GetHashCode()

Returns a hash code for the current result. A Failure returns the hash code of its Exception; a Success returns 0.

Returns — A 32-bit signed integer hash code.

ToString()

public sealed override string ToString()

Returns the message of the captured exception when failed, or string.Empty when successful.

Returns — Exception.Message for a Failure; otherwise an empty string.

Operators

Operator Description
implicit operator Result(Exception) Converts an exception to a failed result.
operator == Determines whether two results are equal.
operator != Determines whether two results are not equal.

implicit operator Result(Exception)

public static implicit operator Result(Exception exception)

Implicitly converts an Exception to a Failure. This is what allows combinator catch blocks to return exception; directly.

operator ==

public static bool operator ==(Result? left, Result? right)

Determines whether two results are equal. Returns true if left equals right; two null references are considered equal.

operator !=

public static bool operator !=(Result? left, Result? right)

Determines whether two results are not equal. Returns true if left does not equal right.

See Also

ONIXLabs .NET Library


Clone this wiki locally