RESTCLIENT.NET / API REFERENCE
Outcome.Result<TSuccess, TFailure>
All API types · Outcome · View source
public abstract partial record Result<TSuccess, TFailure>
A discriminated union representing either a successful result or a failure. This type enforces explicit error handling and eliminates the need for exceptions in business logic, following the Railway Oriented Programming pattern. The Result type is a closed hierarchy - only Ok and Error instances can exist, ensuring complete pattern matching coverage and type safety.
Type parameter TSuccess: The type of the success value.
Type parameter TFailure: The type of the failure value.
Members
- Bind
- Failure
- GetValueOrDefault
- GetValueOrDefault
- MapError
- Map
- Match
- Tap
- op_LogicalNot
- op_UnaryPlus
- IsError
- IsOk
Bind
public Result<TNewSuccess, TFailure> Bind<TNewSuccess>(Func<TSuccess, Result<TNewSuccess, TFailure>> binder);
Monadic bind operation for Result. Applies a function that returns a Result to the success value of this Result, flattening the nested Result. This is the core operation that makes Result a monad, enabling Railway Oriented Programming and safe composition of operations.
| Parameter | Type | Default | Description |
|---|---|---|---|
binder |
Func<TSuccess, Result<TNewSuccess, TFailure>> |
Required | The function to apply if this is a success. |
Type parameter TNewSuccess: The success type of the result returned by the binder.
Returns: The result of applying the binder, or the original error.
View source: Outcome/Result.cs:101
Failure
public static Result<TSuccess, TFailure> Failure(TFailure error);
Creates a failed result containing the specified error. This is the canonical way to create an error in the Result monad.
| Parameter | Type | Default | Description |
|---|---|---|---|
error |
TFailure |
Required | The error value. |
Returns: An Error result containing the error.
View source: Outcome/Result.cs:43
GetValueOrDefault
public TSuccess GetValueOrDefault(Func<TSuccess> defaultProvider);
Returns the success value if this is Ok, otherwise returns the result of calling the provider function. This enables lazy evaluation of default values.
| Parameter | Type | Default | Description |
|---|---|---|---|
defaultProvider |
Func<TSuccess> |
Required | Function to call if this is an Error. |
Returns: The success value or the result of the default provider.
View source: Outcome/Result.cs:174
GetValueOrDefault
public TSuccess GetValueOrDefault(TSuccess defaultValue);
Returns the success value if this is Ok, otherwise returns the provided default value. This is a safe way to extract values from Results with a fallback.
| Parameter | Type | Default | Description |
|---|---|---|---|
defaultValue |
TSuccess |
Required | The value to return if this is an Error. |
Returns: The success value or the default value.
View source: Outcome/Result.cs:165
MapError
public Result<TSuccess, TNewFailure> MapError<TNewFailure>(Func<TFailure, TNewFailure> mapper);
Transforms the error value of this result using the provided function. If this is an Ok, the success value is propagated unchanged. This enables error transformation and recovery scenarios while preserving the success path.
| Parameter | Type | Default | Description |
|---|---|---|---|
mapper |
Func<TFailure, TNewFailure> |
Required | The function to apply to the error value. |
Type parameter TNewFailure: The type of the transformed error value.
Returns: A new result with the original success value or the transformed error.
View source: Outcome/Result.cs:77
Map
public Result<TNewSuccess, TFailure> Map<TNewSuccess>(Func<TSuccess, TNewSuccess> mapper);
Transforms the success value of this result using the provided function. If this is an Error, the error is propagated unchanged. This is the fundamental Functor operation for Result, enabling composition of operations while preserving the error context.
| Parameter | Type | Default | Description |
|---|---|---|---|
mapper |
Func<TSuccess, TNewSuccess> |
Required | The function to apply to the success value. |
Type parameter TNewSuccess: The type of the transformed success value.
Returns: A new result with the transformed success value or the original error.
View source: Outcome/Result.cs:57
Match
public TResult Match<TResult>(Func<TSuccess, TResult> onSuccess, Func<TFailure, TResult> onError);
Applies one of two functions based on the state of this Result. This is the catamorphism for Result, allowing for complete pattern matching. Use this when you need to extract a value of the same type from both success and error cases.
| Parameter | Type | Default | Description |
|---|---|---|---|
onSuccess |
Func<TSuccess, TResult> |
Required | Function to apply if this is a success. |
onError |
Func<TFailure, TResult> |
Required | Function to apply if this is an error. |
Type parameter TResult: The type of value to return.
Returns: The result of applying the appropriate function.
View source: Outcome/Result.cs:121
Tap
public Result<TSuccess, TFailure> Tap(Action<TSuccess>? onSuccess = null, Action<TFailure>? onError = null);
Performs a side effect based on the state of this Result without changing the Result. This enables logging, debugging, or other side effects while preserving the functional nature of the Result chain.
| Parameter | Type | Default | Description |
|---|---|---|---|
onSuccess |
Action<TSuccess>? |
null |
Action to perform if this is a success. |
onError |
Action<TFailure>? |
null |
Action to perform if this is an error. |
Returns: This Result unchanged.
View source: Outcome/Result.cs:139
op_LogicalNot
public static TFailure operator !(Result<TSuccess, TFailure> result);
Unary negation operator to extract the error value if this is an Error, otherwise returns null. This provides a convenient way to extract errors for checking.
| Parameter | Type | Default | Description |
|---|---|---|---|
result |
Result<TSuccess, TFailure> |
Required | The result to extract error from. |
Returns: The error value if Error, null if Ok.
View source: Outcome/Result.cs:184
op_UnaryPlus
public static TSuccess operator +(Result<TSuccess, TFailure> result);
⚠️ Danger. Returns the value or throws if Error. Only for use in tests.
| Parameter | Type | Default | Description |
|---|---|---|---|
result |
Result<TSuccess, TFailure> |
Required | The result to extract the success value from. |
Returns: The success value.
View source: Outcome/Result.cs:199
IsError
public bool IsError { get; }
Gets a value indicating whether this result represents a failed computation. In functional programming, this is analogous to checking if an Either is Left.
View source: Outcome/Result.cs:34
IsOk
public bool IsOk { get; }
Gets a value indicating whether this result represents a successful computation. In functional programming, this is analogous to checking if an Either is Right.