Skip to content

OnixLabs.Core.Result T

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

Result<T> Class

Represents a generic result value that signifies either the successful production of a value of type T or the exception thrown by a failed operation — the value-carrying form of railway-oriented, exception-free error handling.

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

Definition

public closed class Result<T> : IValueEquatable<Result<T>>, IDisposable, IAsyncDisposable

Type Parameters

Parameter Description
T The type of the underlying value carried by a successful result. There is no constraint on T; reference types, value types, and nullable types are all permitted.

Result<T> is a C# 15 closed class with exactly two sealed derivations, Success<T> and Failure<T>. 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<T> is through the static factory members below, the implicit conversions from T or Exception, or the combinators of Result.

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

string message = result switch
{
    Success<int> success => $"Value: {success.Value}",
    Failure<int> 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 no value on success, see Result.

Remarks

Result<T> models the outcome of an operation that either succeeds with a value of type T or fails with an Exception. It is the value-carrying counterpart of Result 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 carrying a value and a failure rail carrying an exception. Combinators such as Select<TResult>(Func<T, TResult>) and SelectMany<TResult>(Func<T, Result<TResult>>) only execute their delegate when the current result is a Success<T>; once a result becomes a Failure<T> it short-circuits, passing the original failure through every subsequent combinator. The captured exception is preserved and never re-evaluated.

Key behaviours and gotchas:

  • Capture, don't throw. Of(Func<T>) and OfAsync invoke a delegate, returning a Success<T> carrying the produced value, or wrapping any thrown Exception in a Failure<T>.
  • Implicit conversions. A value of type T converts implicitly to a Success<T>, and any Exception converts implicitly to a Failure<T>. An explicit conversion to T is also defined, equivalent to GetValueOrThrow().
  • Deferred re-throw. Throw(), GetValueOrThrow(), the explicit T conversion, and GetExceptionOrThrow() let you leave the rails. The captured exception is rethrown with its original stack trace preserved via ExceptionDispatchInfo.
  • IDisposable / IAsyncDisposable. Result<T> is disposable. Disposing a result disposes its underlying value only when the result is a Success<T> and that value implements IDisposable (for Dispose) or IAsyncDisposable (for DisposeAsync). A Failure<T>, or a success whose value is not disposable, is unaffected. This lets a Result<T> of, for example, a Stream participate in a using statement.
  • Value equality. Equality is delegated to ResultEqualityComparer<T>: two Failure<T> instances are equal when their exceptions are equal; two Success<T> instances are equal when their values are equal under the comparer.
  • ToString. Returns Value.ToString() (or string.Empty when the value is null) for a success, or Exception.Message for a failure. Equals, GetHashCode, and ToString are sealed.
  • Async instance methods. Match, Select, and SelectMany each have async overloads (MatchAsync, SelectAsync, SelectManyAsync) declared directly on Result<T>. They accept synchronous, Task-returning, or ValueTask-returning success/failure/selector delegates and a trailing CancellationToken cancellationToken = default; overloads taking ValueTask-returning delegates return ValueTask/ValueTask<TResult>. Further overloads over Task<Result<T>> and ValueTask<Result<T>> receivers are supplied by 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 related types Success<T>, Failure<T>, and ResultEqualityComparer<T> are documented on their own pages.

Examples

Capturing a value-producing operation

using OnixLabs.Core;

Result<int> result = Result<int>.Of(() => int.Parse(userInput));

int value = result.Match(
    success: parsed => parsed,
    failure: _ => -1);

Composing a pipeline

Result<Order> pipeline = Result<string>
    .Of(() => ReadRequestBody())
    .Select(Deserialize)                       // string -> Order
    .SelectMany(order => Validate(order))      // Order  -> Result<Order>
    .Select(order => Enrich(order));           // Order  -> Order

// Any thrown exception short-circuits the remaining steps.
Order order = pipeline.GetValueOrThrow();

Disposing the underlying value

using (Result<FileStream> result = Result<FileStream>.Of(() => File.OpenRead(path)))
{
    result.Match(
        success: stream => Process(stream),
        failure: exception => Log(exception));
} // If the result is a Success<FileStream>, the stream is disposed here.

Static Methods

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

Of(Func<T>)

public static Result<T> Of(Func<T> func)

Invokes the specified function. If it returns without throwing, a Success<T> carrying the returned value is produced; otherwise the thrown Exception is captured as a Failure<T>.

Parameter Type Description
func Func<T> The function to invoke to obtain a successful or failed result.

Returns — A Success<T> carrying the produced value; otherwise a Failure<T>.

OfAsync(Func<Task<T>>, CancellationToken)

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

Asynchronously invokes the specified function, capturing its produced value as a Success<T> or any thrown Exception as a Failure<T>.

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

Returns — A Task<Result<T>> yielding a Success<T> or Failure<T>.

OfAsync(Func<CancellationToken, Task<T>>, CancellationToken)

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

Asynchronously invokes the specified cancellable function, passing cancellationToken to it, capturing its produced value or any thrown exception.

Parameter Type Description
func Func<CancellationToken, Task<T>> 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<T>> yielding a Success<T> or Failure<T>.

Success(T)

public static Success<T> Success(T value)

Creates a result representing a successful outcome carrying the specified value.

Parameter Type Description
value T The underlying successful result value.

Returns — A new Success<T> carrying value.

Failure(Exception)

public static Failure<T> 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<T> 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<T> is in a successful state (that is, whether it is a Success<T>).

IsFailure

public bool IsFailure { get; }

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

Instance Methods

Method Returns Description
Dispose() void Disposes the underlying value if the result is a success and the value is IDisposable.
DisposeAsync() ValueTask Asynchronously disposes the underlying value if the result is a success and the value is IAsyncDisposable.
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.
GetValueOrDefault() T? Gets the underlying value, or default if failed.
GetValueOrDefault(T) T Gets the underlying value, or a specified default if failed.
GetValueOrThrow() T Gets the underlying value, or rethrows the captured exception if failed.
GetValue(out T) Result<T> Gets the underlying value as an out parameter and returns the result for chaining.
Match(Action<T>, Action<Exception>) void Executes the delegate that matches the current state.
Match<TResult>(Func<T, 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. Overloads accept synchronous, Task-returning, or ValueTask-returning success/failure delegates.
Select(Action<T>) Result Applies a selector to the value if successful, returning a Result.
Select<TResult>(Func<T, TResult>) Result<TResult> Applies a value-producing selector if successful.
SelectAsync(...) Task<Result> / Task<Result<TResult>> / ValueTask<Result> / ValueTask<Result<TResult>> Asynchronously applies a selector if successful. Overloads accept Task-returning or ValueTask-returning selectors.
SelectMany(Func<T, Result>) Result Applies a Result-producing selector if successful.
SelectMany<TResult>(Func<T, 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<T>?) 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 value's string, or the failure message.

Dispose()

public void Dispose()

Disposes the underlying value when the current result is a Success<T> whose Value implements IDisposable. Otherwise no disposal occurs. Suppresses finalization on completion.

DisposeAsync()

public async ValueTask DisposeAsync()

Asynchronously disposes the underlying value when the current result is a Success<T> whose Value implements IAsyncDisposable. Otherwise no disposal occurs. Suppresses finalization on completion.

Returns — A ValueTask representing the asynchronous dispose operation.

GetExceptionOrDefault()

public Exception? GetExceptionOrDefault()

Gets the underlying exception if the current Result<T> is a Failure<T>; 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<T> is a Failure<T>; 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 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<T> is a Failure<T>; 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.

GetValueOrDefault()

public T? GetValueOrDefault()

Gets the underlying value if the current Result<T> is a Success<T>; otherwise the default value of T.

Returns — The underlying value, or default(T) when failed.

GetValueOrDefault(T)

public T GetValueOrDefault(T defaultValue)

Gets the underlying value if the current Result<T> is a Success<T>; otherwise returns defaultValue.

Parameter Type Description
defaultValue T The default value to return when the underlying value is absent.

Returns — The underlying value, or defaultValue when failed.

GetValueOrThrow()

public T GetValueOrThrow()

Gets the underlying value if the current Result<T> is a Success<T>; otherwise rethrows the captured exception, preserving its original stack trace via ExceptionDispatchInfo.

Returns — The underlying value.

Exceptions

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

GetValue(out T)

public Result<T> GetValue(out T value)

Sets value to the underlying value when successful, or default! when failed, and returns the current result so calls can be chained.

Parameter Type Description
value out T Receives the underlying value, or default when failed.

Returns — The current Result<T> instance.

Match(Action<T>, Action<Exception>)

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

Executes the delegate that matches the current state. If the result is a Success<T>, success is invoked with the value; if a Failure<T>, failure is invoked with the exception. Both delegates are optional.

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

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

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

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

Parameter Type Description
success Func<T, 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 applied to the value when successful; otherwise the result of failure applied to the exception.

MatchAsync Overloads

Asynchronously executes the delegate that matches the state of the current Result<T>. 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<T, Task>?, Action<Exception>?, CancellationToken) Task success: Task-returning; failure: synchronous
MatchAsync(Func<T, ValueTask>?, Action<Exception>?, CancellationToken) ValueTask success: ValueTask-returning; failure: synchronous
MatchAsync(Func<T, CancellationToken, Task>?, Action<Exception>?, CancellationToken) Task success: Task-returning (cancellable); failure: synchronous
MatchAsync(Func<T, CancellationToken, ValueTask>?, Action<Exception>?, CancellationToken) ValueTask success: ValueTask-returning (cancellable); failure: synchronous
MatchAsync(Action<T>?, Func<Exception, Task>?, CancellationToken) Task success: synchronous; failure: Task-returning
MatchAsync(Action<T>?, Func<Exception, ValueTask>?, CancellationToken) ValueTask success: synchronous; failure: ValueTask-returning
MatchAsync(Action<T>?, Func<Exception, CancellationToken, Task>?, CancellationToken) Task success: synchronous; failure: Task-returning (cancellable)
MatchAsync(Action<T>?, Func<Exception, CancellationToken, ValueTask>?, CancellationToken) ValueTask success: synchronous; failure: ValueTask-returning (cancellable)
MatchAsync(Func<T, Task>?, Func<Exception, Task>?, CancellationToken) Task success: Task-returning; failure: Task-returning
MatchAsync(Func<T, ValueTask>?, Func<Exception, ValueTask>?, CancellationToken) ValueTask success: ValueTask-returning; failure: ValueTask-returning
MatchAsync(Func<T, CancellationToken, Task>?, Func<Exception, CancellationToken, Task>?, CancellationToken) Task success: Task-returning (cancellable); failure: Task-returning (cancellable)
MatchAsync(Func<T, CancellationToken, ValueTask>?, Func<Exception, CancellationToken, ValueTask>?, CancellationToken) ValueTask success: ValueTask-returning (cancellable); failure: ValueTask-returning (cancellable)
MatchAsync<TResult>(Func<T, Task<TResult>>, Func<Exception, TResult>, CancellationToken) Task<TResult> success: Task-returning; failure: synchronous
MatchAsync<TResult>(Func<T, ValueTask<TResult>>, Func<Exception, TResult>, CancellationToken) ValueTask<TResult> success: ValueTask-returning; failure: synchronous
MatchAsync<TResult>(Func<T, CancellationToken, Task<TResult>>, Func<Exception, TResult>, CancellationToken) Task<TResult> success: Task-returning (cancellable); failure: synchronous
MatchAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>>, Func<Exception, TResult>, CancellationToken) ValueTask<TResult> success: ValueTask-returning (cancellable); failure: synchronous
MatchAsync<TResult>(Func<T, TResult>, Func<Exception, Task<TResult>>, CancellationToken) Task<TResult> success: synchronous; failure: Task-returning
MatchAsync<TResult>(Func<T, TResult>, Func<Exception, ValueTask<TResult>>, CancellationToken) ValueTask<TResult> success: synchronous; failure: ValueTask-returning
MatchAsync<TResult>(Func<T, TResult>, Func<Exception, CancellationToken, Task<TResult>>, CancellationToken) Task<TResult> success: synchronous; failure: Task-returning (cancellable)
MatchAsync<TResult>(Func<T, TResult>, Func<Exception, CancellationToken, ValueTask<TResult>>, CancellationToken) ValueTask<TResult> success: synchronous; failure: ValueTask-returning (cancellable)
MatchAsync<TResult>(Func<T, Task<TResult>>, Func<Exception, Task<TResult>>, CancellationToken) Task<TResult> success: Task-returning; failure: Task-returning
MatchAsync<TResult>(Func<T, ValueTask<TResult>>, Func<Exception, ValueTask<TResult>>, CancellationToken) ValueTask<TResult> success: ValueTask-returning; failure: ValueTask-returning
MatchAsync<TResult>(Func<T, CancellationToken, Task<TResult>>, Func<Exception, CancellationToken, Task<TResult>>, CancellationToken) Task<TResult> success: Task-returning (cancellable); failure: Task-returning (cancellable)
MatchAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>>, Func<Exception, CancellationToken, ValueTask<TResult>>, CancellationToken) ValueTask<TResult> success: ValueTask-returning (cancellable); failure: ValueTask-returning (cancellable)
public async Task MatchAsync(Func<T, Task>? success = null, Action<Exception>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<T, ValueTask>? success = null, Action<Exception>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Func<T, CancellationToken, Task>? success = null, Action<Exception>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<T, CancellationToken, ValueTask>? success = null, Action<Exception>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Action<T>? success = null, Func<Exception, Task>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Action<T>? success = null, Func<Exception, ValueTask>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Action<T>? success = null, Func<Exception, CancellationToken, Task>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Action<T>? success = null, Func<Exception, CancellationToken, ValueTask>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Func<T, Task>? success = null, Func<Exception, Task>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<T, ValueTask>? success = null, Func<Exception, ValueTask>? failure = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Func<T, CancellationToken, Task>? success = null, Func<Exception, CancellationToken, Task>? failure = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<T, CancellationToken, ValueTask>? success = null, Func<Exception, CancellationToken, ValueTask>? failure = null, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, Task<TResult>> success, Func<Exception, TResult> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, ValueTask<TResult>> success, Func<Exception, TResult> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, CancellationToken, Task<TResult>> success, Func<Exception, TResult> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>> success, Func<Exception, TResult> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, TResult> success, Func<Exception, Task<TResult>> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, TResult> success, Func<Exception, ValueTask<TResult>> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, TResult> success, Func<Exception, CancellationToken, Task<TResult>> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, TResult> success, Func<Exception, CancellationToken, ValueTask<TResult>> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, Task<TResult>> success, Func<Exception, Task<TResult>> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, ValueTask<TResult>> success, Func<Exception, ValueTask<TResult>> failure, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, CancellationToken, Task<TResult>> success, Func<Exception, CancellationToken, Task<TResult>> failure, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, 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<T> instance is in a successful state.
failure The delegate to execute when the current Result<T> 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<T> instance is in a successful state; otherwise, returns the result of the failure delegate if the current Result<T> 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<T>)

public Result Select(Action<T> selector)

If the current result is not a Success<T>, the captured exception is returned as an untyped Failure. Otherwise selector is invoked with the value: a Success is returned if it completes, or a Failure wrapping any thrown exception.

Parameter Type Description
selector Action<T> The delegate to apply to the value of the current result.

Returns — An untyped Result representing the outcome.

Select<TResult>(Func<T, TResult>)

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

If the current result is not a Success<T>, the captured exception is returned as a Failure<TResult>. Otherwise selector is invoked with the value: its return value becomes a Success<TResult>, or any thrown exception becomes a Failure<TResult>.

Parameter Type Description
selector Func<T, TResult> The value-producing delegate to apply to the value of the current result.

Returns — A Result<TResult> representing the outcome.

SelectAsync Overloads

Asynchronously applies the selector delegate to the underlying value if the current Result<T> 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<T, Task>, CancellationToken) Task<Result> selector: Task-returning
SelectAsync(Func<T, ValueTask>, CancellationToken) ValueTask<Result> selector: ValueTask-returning
SelectAsync(Func<T, CancellationToken, Task>, CancellationToken) Task<Result> selector: Task-returning (cancellable)
SelectAsync(Func<T, CancellationToken, ValueTask>, CancellationToken) ValueTask<Result> selector: ValueTask-returning (cancellable)
SelectAsync<TResult>(Func<T, Task<TResult>>, CancellationToken) Task<Result<TResult>> selector: Task-returning
SelectAsync<TResult>(Func<T, ValueTask<TResult>>, CancellationToken) ValueTask<Result<TResult>> selector: ValueTask-returning
SelectAsync<TResult>(Func<T, CancellationToken, Task<TResult>>, CancellationToken) Task<Result<TResult>> selector: Task-returning (cancellable)
SelectAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>>, CancellationToken) ValueTask<Result<TResult>> selector: ValueTask-returning (cancellable)
public async Task<Result> SelectAsync(Func<T, Task> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result> SelectAsync(Func<T, ValueTask> selector, CancellationToken cancellationToken = default)
public async Task<Result> SelectAsync(Func<T, CancellationToken, Task> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result> SelectAsync(Func<T, CancellationToken, ValueTask> selector, CancellationToken cancellationToken = default)
public async Task<Result<TResult>> SelectAsync<TResult>(Func<T, Task<TResult>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result<TResult>> SelectAsync<TResult>(Func<T, ValueTask<TResult>> selector, CancellationToken cancellationToken = default)
public async Task<Result<TResult>> SelectAsync<TResult>(Func<T, CancellationToken, Task<TResult>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result<TResult>> SelectAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken = default)

Parameters

Parameter Description
selector The delegate to apply to the value of the current Result<T> 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<T> 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<T> 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<T, Result>)

public Result SelectMany(Func<T, Result> selector)

If the current result is not a Success<T>, the captured exception is returned as an untyped Failure. Otherwise selector is invoked with the value and its Result is returned; any thrown exception is captured as a Failure.

Parameter Type Description
selector Func<T, Result> The Result-producing delegate to apply to the value of the current result.

Returns — An untyped Result representing the outcome.

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

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

If the current result is not a Success<T>, the captured exception is returned as a Failure<TResult>. Otherwise selector is invoked with the value and its Result<TResult> is returned; any thrown exception is captured as a Failure<TResult>.

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

Returns — A Result<TResult> representing the outcome.

SelectManyAsync Overloads

Asynchronously applies a Result-producing selector to the underlying value if the current Result<T> 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<T, Task<Result>>, CancellationToken) Task<Result> selector: Task-returning
SelectManyAsync(Func<T, ValueTask<Result>>, CancellationToken) ValueTask<Result> selector: ValueTask-returning
SelectManyAsync(Func<T, CancellationToken, Task<Result>>, CancellationToken) Task<Result> selector: Task-returning (cancellable)
SelectManyAsync(Func<T, CancellationToken, ValueTask<Result>>, CancellationToken) ValueTask<Result> selector: ValueTask-returning (cancellable)
SelectManyAsync<TResult>(Func<T, Task<Result<TResult>>>, CancellationToken) Task<Result<TResult>> selector: Task-returning
SelectManyAsync<TResult>(Func<T, ValueTask<Result<TResult>>>, CancellationToken) ValueTask<Result<TResult>> selector: ValueTask-returning
SelectManyAsync<TResult>(Func<T, CancellationToken, Task<Result<TResult>>>, CancellationToken) Task<Result<TResult>> selector: Task-returning (cancellable)
SelectManyAsync<TResult>(Func<T, CancellationToken, ValueTask<Result<TResult>>>, CancellationToken) ValueTask<Result<TResult>> selector: ValueTask-returning (cancellable)
public async Task<Result> SelectManyAsync(Func<T, Task<Result>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result> SelectManyAsync(Func<T, ValueTask<Result>> selector, CancellationToken cancellationToken = default)
public async Task<Result> SelectManyAsync(Func<T, CancellationToken, Task<Result>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result> SelectManyAsync(Func<T, CancellationToken, ValueTask<Result>> selector, CancellationToken cancellationToken = default)
public async Task<Result<TResult>> SelectManyAsync<TResult>(Func<T, Task<Result<TResult>>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result<TResult>> SelectManyAsync<TResult>(Func<T, ValueTask<Result<TResult>>> selector, CancellationToken cancellationToken = default)
public async Task<Result<TResult>> SelectManyAsync<TResult>(Func<T, CancellationToken, Task<Result<TResult>>> selector, CancellationToken cancellationToken = default)
public async ValueTask<Result<TResult>> SelectManyAsync<TResult>(Func<T, CancellationToken, ValueTask<Result<TResult>>> selector, CancellationToken cancellationToken = default)

Parameters

Parameter Description
selector The delegate to apply to the value of the current Result<T> 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<T> 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<T> 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<T>, rethrows the captured exception with its original stack trace preserved via ExceptionDispatchInfo; otherwise does nothing.

Exceptions

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

Equals(Result<T>?)

public bool Equals(Result<T>? other)

Determines whether the current result equals another, delegating to ResultEqualityComparer<T>.Default. Two failures are equal when their exceptions are equal; two successes are equal when their values 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<T>?) when obj is a Result<T>.

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

GetHashCode()

public sealed override int GetHashCode()

Returns a hash code for the current result. A Success<T> returns the hash code of its value (or 0 when the value is null); a Failure<T> returns the hash code of its exception.

Returns — A 32-bit signed integer hash code.

ToString()

public sealed override string ToString()

Returns the string representation of the underlying value (or string.Empty when the value is null) for a Success<T>, or the message of the captured exception for a Failure<T>.

Returns — Value.ToString()/empty string when successful; otherwise Exception.Message.

Operators

Operator Description
implicit operator Result<T>(T) Converts a value to a successful result.
implicit operator Result<T>(Exception) Converts an exception to a failed result.
explicit operator T(Result<T>) Extracts the underlying value, or rethrows the captured exception.
operator == Determines whether two results are equal.
operator != Determines whether two results are not equal.

implicit operator Result<T>(T)

public static implicit operator Result<T>(T value)

Implicitly converts a value of type T to a Success<T>.

implicit operator Result<T>(Exception)

public static implicit operator Result<T>(Exception exception)

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

explicit operator T(Result<T>)

public static explicit operator T(Result<T> value)

Extracts the underlying value of the result. Equivalent to GetValueOrThrow(): rethrows the captured exception (preserving its stack trace) when the result is failed.

Exceptions

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

operator ==

public static bool operator ==(Result<T>? left, Result<T>? 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<T>? left, Result<T>? 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