RESTCLIENT.NET
Menu
EN
↗ GitHub

RESTCLIENT.NET / API REFERENCE

Outcome.ResultExtensions

All API types · Outcome · View source

public static class ResultExtensions

Extension methods that provide additional functional programming utilities for Result. These methods enable more fluent and expressive Result-based code.

Members

Combine

public static Result<TSuccess, TFailure> Combine<TSuccess1, TSuccess2, TSuccess, TFailure>(this Result<TSuccess1, TFailure> result1, Result<TSuccess2, TFailure> result2, Func<TSuccess1, TSuccess2, TSuccess> combiner);

Combines two Results using a combining function. Both Results must be Ok for the combination to succeed.

Parameter Type Default Description
result1 Result<TSuccess1, TFailure> Required The first Result.
result2 Result<TSuccess2, TFailure> Required The second Result.
combiner Func<TSuccess1, TSuccess2, TSuccess> Required Function to combine the two success values.

Type parameter TSuccess1: The success type of the first Result.

Type parameter TSuccess2: The success type of the second Result.

Type parameter TSuccess: The success type of the combined Result.

Type parameter TFailure: The failure type.

Returns: Ok with combined value if both are Ok, otherwise the first Error.

View source: Outcome/ResultExtensions.cs:69

Filter

public static Result<TSuccess, TFailure> Filter<TSuccess, TFailure>(this Result<TSuccess, TFailure> result, Func<TSuccess, bool> predicate, TFailure errorOnFalse);

Filters a Result based on a predicate. If the Result is Ok and the predicate returns true, returns the original Result. If the Result is Ok but the predicate returns false, returns Error with the provided error. If the Result is already Error, returns it unchanged.

Parameter Type Default Description
result Result<TSuccess, TFailure> Required The Result to filter.
predicate Func<TSuccess, bool> Required The predicate to test the success value.
errorOnFalse TFailure Required The error to return if the predicate fails.

Type parameter TSuccess: The success type.

Type parameter TFailure: The failure type.

Returns: The original Result if predicate passes, Error if predicate fails.

View source: Outcome/ResultExtensions.cs:87

Flatten

public static Result<TSuccess, TFailure> Flatten<TSuccess, TFailure>(this Result<Result<TSuccess, TFailure>, TFailure> result);

Flattens a nested Result structure. This is useful when you have Result<Result<T, E>, E> and want Result<T, E>.

Parameter Type Default Description
result Result<Result<TSuccess, TFailure>, TFailure> Required The nested Result to flatten.

Type parameter TSuccess: The success type.

Type parameter TFailure: The failure type.

Returns: A flattened Result.

View source: Outcome/ResultExtensions.cs:53

GetErrorOrThrow

public static TFailure GetErrorOrThrow<TSuccess, TFailure>(this Result<TSuccess, TFailure> result, string errorMessage = "Expected error result");

⚠️ Dangerous Method - Use with Caution! ⚠️ Extracts the error value from a Result, or throws an InvalidOperationException if the Result is a success. This is useful in test scenarios where you expect an error and want to fail the test if a success occurs.

Parameter Type Default Description
result Result<TSuccess, TFailure> Required The Result to extract the error from.
errorMessage string "Expected error result" Optional custom error message to use when throwing. Defaults to "Expected error result".

Type parameter TSuccess: The success type.

Type parameter TFailure: The failure type.

Returns: The failure value.

View source: Outcome/ResultExtensions.cs:125

GetValueOrThrow

public static TSuccess GetValueOrThrow<TSuccess, TFailure>(this Result<TSuccess, TFailure> result, string errorMessage = "Expected success result");

⚠️ Dangerous Method - Use with Caution! ⚠️ Extracts the success value from a Result, or throws an InvalidOperationException if the Result is an Error. This is useful in test scenarios where you expect a success and want to fail the test if an error occurs.

Parameter Type Default Description
result Result<TSuccess, TFailure> Required The Result to extract the value from.
errorMessage string "Expected success result" Optional custom error message to use when throwing. Defaults to "Expected success result".

Type parameter TSuccess: The success type.

Type parameter TFailure: The failure type.

Returns: The success value.

View source: Outcome/ResultExtensions.cs:109

Sequence

public static Result<IReadOnlyList<TSuccess>, TFailure> Sequence<TSuccess, TFailure>(this IEnumerable<Result<TSuccess, TFailure>> results);

Transforms a sequence of Results into a Result of a sequence. If all Results are Ok, returns Ok with all the values. If any Result is Error, returns the first Error encountered. This is the sequence/traverse operation for Result, enabling all-or-nothing semantics for collections of Results.

Parameter Type Default Description
results IEnumerable<Result<TSuccess, TFailure>> Required The sequence of Results to traverse.

Type parameter TSuccess: The success type.

Type parameter TFailure: The failure type.

Returns: A Result containing all success values or the first error.

View source: Outcome/ResultExtensions.cs:21