Result Struct
Definition
Represents the outcome of an operation that produces no value: either Success, or Failure(error)
carrying a ResultError.
public readonly struct Result : IEquatable<Result>
- Implements
- Inherited Members
- Extension Methods
Remarks
Result makes failure explicit in a signature without resorting to exceptions for expected error paths. Collapse to a final value with Match<TResult>(Func<TResult>, Func<ResultError, TResult>), or branch with Match(Action, Action<ResultError>). The type also hosts the factory surface for the whole family, including the generic Result<T> factories Success<T>(T) and Failure<T>(ResultError).
default(Result) equals a failure carrying an empty ResultError - a result that was never
assigned behaves exactly like an explicit failure, so the type is total and safe to use as a field or array element
without initialization. Error on default(Result) is valid and returns the empty error.
Properties
Error
Gets the error carried by a failed result.
public ResultError Error { get; }
Property Value
- ResultError
The ResultError describing the failure.
Remarks
Prefer TryGetError(out ResultError) or
Match<TResult>(Func<TResult>, Func<ResultError, TResult>) over direct access when success is an
expected state; Error is intended for call sites that have already established
IsFailure. On default(Result) it is valid and returns the empty error.
Exceptions
- InvalidOperationException
The result does not represent a failure.
IsFailure
Gets a value indicating whether this result represents failure.
public bool IsFailure { get; }
Property Value
IsSuccess
Gets a value indicating whether this result represents success.
public bool IsSuccess { get; }
Property Value
Methods
Equals(Result)
Determines whether the specified result is equal to the current instance.
public bool Equals(Result other)
Parameters
otherResultThe result to compare with this instance.
Returns
- bool
true if both are successes, or both are failures whose errors compare equal using Equals(ResultError); otherwise, false.
Equals(object?)
Determines whether the specified object is equal to the current result.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare. Only another Result can be equal; all other types, including null, return false.
Returns
Failure(ResultError)
Creates a result representing failure with the specified error.
public static Result Failure(ResultError error)
Parameters
errorResultErrorThe error describing the failure.
Returns
Failure<T>(ResultError)
Creates a failed result of the specified value type carrying the specified error.
public static Result<T> Failure<T>(ResultError error)
Parameters
errorResultErrorThe error describing the failure.
Returns
Type Parameters
TThe value type of the result.
GetHashCode()
Returns a hash code for the current instance consistent with Equals(Result).
public override int GetHashCode()
Returns
- int
1for success; otherwise the error's hash code.
Match(Action, Action<ResultError>)
Invokes the matching action for the state of the result.
public void Match(Action onSuccess, Action<ResultError> onFailure)
Parameters
onSuccessActionThe action invoked when the result represents success.
onFailureAction<ResultError>The action invoked with the error when the result represents failure.
Exceptions
- ArgumentNullException
onSuccessoronFailureis null.
Match<TResult>(Func<TResult>, Func<ResultError, TResult>)
Collapses the result to a single value by invoking the matching branch.
public TResult Match<TResult>(Func<TResult> onSuccess, Func<ResultError, TResult> onFailure)
Parameters
onSuccessFunc<TResult>The factory invoked when the result represents success.
onFailureFunc<ResultError, TResult>The projection invoked with the error when the result represents failure.
Returns
- TResult
The value produced by the invoked branch.
Type Parameters
TResultThe type produced by both branches.
Examples
var label = result.Match(() => "ok", error => $"failed: {error}");
Exceptions
- ArgumentNullException
onSuccessoronFailureis null.
Success()
Creates a result representing success.
public static Result Success()
Returns
Examples
var ok = Result.Success(); // Success
var failed = Result.Failure(ResultError.FromMessage("boom")); // Failure(boom)
Success<T>(T)
Creates a successful result carrying the specified non-null value.
public static Result<T> Success<T>(T value)
Parameters
valueTThe value to wrap. Must not be null.
Returns
Type Parameters
TThe type of the value.
Remarks
This is a strict lift: a null argument is rejected because a successful result must always carry a value, mirroring Some(T).
Exceptions
- ArgumentNullException
valueis null.
ToString()
Returns a string representation of the result.
public override string ToString()
Returns
- string
"Success"for success; otherwise"Failure(error)".
TryGetError(out ResultError)
Attempts to retrieve the error carried by a failed result.
public bool TryGetError(out ResultError error)
Parameters
errorResultErrorWhen this method returns, the error if the result represents failure; otherwise the default error.
Returns
Operators
operator ==(Result, Result)
Determines whether two results are equal.
public static bool operator ==(Result left, Result right)
Parameters
Returns
operator !=(Result, Result)
Determines whether two results are not equal.
public static bool operator !=(Result left, Result right)
Parameters
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |