Table of Contents

Struct Result<TValue, TFailure>

Namespace
Badeend
Assembly
Badeend.Result.dll

Represents the result of a fallible operation.

A Result can be in one of two states: Success or Failure. Both states have an associated payload of type TValue or TFailure respectively.

public readonly struct Result<TValue, TFailure> : IEquatable<Result<TValue, TFailure>>, IComparable<Result<TValue, TFailure>>, IComparable

Type Parameters

TValue

Type of the result when the operation succeeds.

TFailure

Type of the result when the operation fails.

Implements
IEquatable<Result<TValue, TFailure>>
IComparable<Result<TValue, TFailure>>
Inherited Members

Remarks

Because of the implicit conversion operators you typically don't have to manually construct Results. If you do want or need to, you can use Result.Success() or Result.Failure() instead.

You can examine a result like this:

_ = myResult.State switch
{
  ResultState.Success => $"Something successful: {myResult.Value}",
  ResultState.Failure => $"Something failed: {myResult.Failure}",
};

Or alternatively using IsSuccess, IsFailure, TryGetValue, TryGetFailure, GetValueOrDefault or GetFailureOrDefault.

A Result's default value is equivalent to Result.Failure(default!).

Properties

Failure

Get the failure value.

public TFailure Failure { get; }

Property Value

TFailure

Exceptions

InvalidOperationException

The operation did not fail.

IsFailure

Check whether the operation failed.

[Pure]
public bool IsFailure { get; }

Property Value

bool

IsSuccess

Check whether the operation succeeded.

[Pure]
public bool IsSuccess { get; }

Property Value

bool

State

Get the state of the result (Success or Failure).

[Pure]
public ResultState State { get; }

Property Value

ResultState

Value

Get the success value.

public TValue Value { get; }

Property Value

TValue

Exceptions

InvalidOperationException

The operation was not successful.

Methods

CompareTo(Result<TValue, TFailure>)

Compare two results.

Successful results precede failed results.

[Pure]
public int CompareTo(Result<TValue, TFailure> other)

Parameters

other Result<TValue, TFailure>

Returns

int

See IComparable<T>.CompareTo(T) for more information.

Equals(Result<TValue, TFailure>)

Check for equality.

[Pure]
public bool Equals(Result<TValue, TFailure> other)

Parameters

other Result<TValue, TFailure>

Returns

bool

GetFailureOrDefault()

Attempt to get the operation's failure value. Returns default when the operation succeeded.

[Pure]
public TFailure? GetFailureOrDefault()

Returns

TFailure

GetFailureOrDefault(TFailure)

Attempt to get the operation's failure value. Returns defaultValue when the operation succeeded.

[Pure]
public TFailure GetFailureOrDefault(TFailure defaultValue)

Parameters

defaultValue TFailure

Returns

TFailure

GetHashCode()

Returns the hash code for this instance.

[Pure]
public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

GetValueOrDefault()

Attempt to get the operation's success value. Returns default when the operation failed.

[Pure]
public TValue? GetValueOrDefault()

Returns

TValue

GetValueOrDefault(TValue)

Attempt to get the operation's success value. Returns defaultValue when the operation failed.

[Pure]
public TValue GetValueOrDefault(TValue defaultValue)

Parameters

defaultValue TValue

Returns

TValue

ToString()

Get a string representation of the result for debugging purposes. The format is not stable and may change without prior notice.

[Pure]
public override string ToString()

Returns

string

TryGetFailure(out TFailure)

Attempt to store the operation's failure in failure. Returns false when the operation succeeded.

public bool TryGetFailure(out TFailure failure)

Parameters

failure TFailure

Returns

bool

TryGetValue(out TValue)

Attempt to store the operation's success value in value. Returns false when the operation failed.

public bool TryGetValue(out TValue value)

Parameters

value TValue

Returns

bool

Operators

operator ==(Result<TValue, TFailure>, Result<TValue, TFailure>)

Check for equality.

[Pure]
public static bool operator ==(Result<TValue, TFailure> left, Result<TValue, TFailure> right)

Parameters

left Result<TValue, TFailure>
right Result<TValue, TFailure>

Returns

bool

implicit operator Result<TValue, TFailure>(TValue)

Create a successful result.

[Pure]
public static implicit operator Result<TValue, TFailure>(TValue value)

Parameters

value TValue

Returns

Result<TValue, TFailure>

implicit operator Result<TValue, TFailure>(TFailure)

Create a failure result.

[Pure]
public static implicit operator Result<TValue, TFailure>(TFailure failure)

Parameters

failure TFailure

Returns

Result<TValue, TFailure>

operator !=(Result<TValue, TFailure>, Result<TValue, TFailure>)

Check for inequality.

[Pure]
public static bool operator !=(Result<TValue, TFailure> left, Result<TValue, TFailure> right)

Parameters

left Result<TValue, TFailure>
right Result<TValue, TFailure>

Returns

bool