Show / Hide Table of Contents

Struct Result<T, TError>

Represents a result of operation which can be actual result or error code.

Implements
IResultMonad<T, TError, Result<T, TError>>
IResultMonad<T, TError>
IOptionMonad<T, Result<T, TError>>
IOptionMonad<T>
ISupplier<object>
IFunctional
Inherited Members
ValueType.Equals(object)
ValueType.GetHashCode()
object.GetType()
object.Equals(object, object)
object.ReferenceEquals(object, object)
Namespace: DotNext
Assembly: DotNext.dll
Syntax
public readonly struct Result<T, TError> : IResultMonad<T, TError, Result<T, TError>>, IResultMonad<T, TError>, IOptionMonad<T, Result<T, TError>>, IOptionMonad<T>, ISupplier<object?>, IFunctional where TError : struct, Enum
Type Parameters
Name Description
T

The type of the result.

TError

The type of the error code. Default value must represent the successful result.

Constructors

View Source

Result(T)

Initializes a new successful result.

Declaration
public Result(T value)
Parameters
Type Name Description
T value

The result value.

View Source

Result(TError)

Initializes a new unsuccessful result.

Declaration
public Result(TError error)
Parameters
Type Name Description
TError error

The error code.

Exceptions
Type Condition
ArgumentOutOfRangeException

error represents a successful code.

Properties

View Source

Error

Gets the error code.

Declaration
public TError Error { get; }
Property Value
Type Description
TError
View Source

IsSuccessful

Indicates that the result is successful.

Declaration
public bool IsSuccessful { get; }
Property Value
Type Description
bool

true if this result is successful; false if this result represents exception.

View Source

Value

Extracts the actual result.

Declaration
public T Value { get; }
Property Value
Type Description
T
Exceptions
Type Condition
UndefinedResultException<TError>

The value is unavailable.

View Source

ValueOrDefault

Returns the value if present; otherwise return default value.

Declaration
public T? ValueOrDefault { get; }
Property Value
Type Description
T

The value, if present, otherwise default.

View Source

ValueRef

Gets a reference to the underlying value.

Declaration
[UnscopedRef]
[JsonIgnore]
public ref readonly T ValueRef { get; }
Property Value
Type Description
T

The reference to the result.

Exceptions
Type Condition
UndefinedResultException<TError>

The value is unavailable.

Methods

View Source

Box()

Gets boxed representation of the result.

Declaration
public Result<object?, TError> Box()
Returns
Type Description
Result<object, TError>

The boxed representation of the result.

View Source

Convert<TResult>(delegate*<T, Result<TResult, TError>>)

If successful result is present, apply the provided mapping function. If not, forward the error.

Declaration
[CLSCompliant(false)]
public Result<TResult, TError> Convert<TResult>(delegate*<T, Result<TResult, TError>> converter)
Parameters
Type Name Description
delegate*<T, Result<TResult, TError>> converter

A mapping function to be applied to the value, if present.

Returns
Type Description
Result<TResult, TError>

The conversion result.

Type Parameters
Name Description
TResult

The type of the result of the mapping function.

View Source

Convert<TResult>(delegate*<T, Result<TResult, TError>>)

If successful result is present, apply the provided mapping function. If not, forward the error.

Declaration
[CLSCompliant(false)]
public Result<TResult, TError> Convert<TResult>(delegate*<T, Result<TResult, TError>> converter)
Parameters
Type Name Description
delegate*<T, Result<TResult, TError>> converter

A mapping function to be applied to the value, if present.

Returns
Type Description
Result<TResult, TError>

The conversion result.

Type Parameters
Name Description
TResult

The type of the result of the mapping function.

View Source

Convert<TResult>(Converter<T, Result<TResult, TError>>)

If successful result is present, apply the provided mapping function. If not, forward the error.

Declaration
public Result<TResult, TError> Convert<TResult>(Converter<T, Result<TResult, TError>> converter)
Parameters
Type Name Description
Converter<T, Result<TResult, TError>> converter

A mapping function to be applied to the value, if present.

Returns
Type Description
Result<TResult, TError>

The conversion result.

Type Parameters
Name Description
TResult

The type of the result of the mapping function.

View Source

Convert<TResult>(Converter<T, TResult>)

If the successful result is present, apply the provided mapping function hiding any exception caused by the converter.

Declaration
public Result<TResult, TError> Convert<TResult>(Converter<T, TResult> converter)
Parameters
Type Name Description
Converter<T, TResult> converter

A mapping function to be applied to the value, if present.

Returns
Type Description
Result<TResult, TError>

The conversion result.

Type Parameters
Name Description
TResult

The type of the mapping function result.

View Source

Or(T?)

Returns the value if present; otherwise return default value.

Declaration
public T? Or(T? defaultValue)
Parameters
Type Name Description
T defaultValue

The value to be returned if this result is unsuccessful.

Returns
Type Description
T

The value, if present, otherwise defaultValue.

View Source

OrInvoke(delegate*<TError, T>)

Returns the value if present; otherwise invoke delegate.

Declaration
[CLSCompliant(false)]
public T OrInvoke(delegate*<TError, T> defaultFunc)
Parameters
Type Name Description
delegate*<TError, T> defaultFunc

A delegate to be invoked if value is not present.

Returns
Type Description
T

The value, if present, otherwise returned from delegate.

View Source

OrInvoke(delegate*<TError, T>)

Returns the value if present; otherwise invoke delegate.

Declaration
[CLSCompliant(false)]
public T OrInvoke(delegate*<TError, T> defaultFunc)
Parameters
Type Name Description
delegate*<TError, T> defaultFunc

A delegate to be invoked if value is not present.

Returns
Type Description
T

The value, if present, otherwise returned from delegate.

View Source

OrInvoke(Func<T>)

Returns the value if present; otherwise invoke delegate.

Declaration
public T OrInvoke(Func<T> defaultFunc)
Parameters
Type Name Description
Func<T> defaultFunc

A delegate to be invoked if value is not present.

Returns
Type Description
T

The value, if present, otherwise returned from delegate.

View Source

OrInvoke(Func<TError, T>)

Returns the value if present; otherwise invoke delegate.

Declaration
public T OrInvoke(Func<TError, T> defaultFunc)
Parameters
Type Name Description
Func<TError, T> defaultFunc

A delegate to be invoked if value is not present.

Returns
Type Description
T

The value, if present, otherwise returned from delegate.

View Source

OrThrow(delegate*<TError, Exception>)

Gets underlying value or throws an exception.

Declaration
[CLSCompliant(false)]
public T OrThrow(delegate*<TError, Exception> exceptionFactory)
Parameters
Type Name Description
delegate*<TError, Exception> exceptionFactory

The exception factory that accepts the error code.

Returns
Type Description
T

The underlying value.

Exceptions
Type Condition
Exception

The result is unsuccessful.

View Source

OrThrow(Func<TError, Exception>)

Gets underlying value or throws an exception.

Declaration
public T OrThrow(Func<TError, Exception> exceptionFactory)
Parameters
Type Name Description
Func<TError, Exception> exceptionFactory

The exception factory that accepts the error code.

Returns
Type Description
T

The underlying value.

Exceptions
Type Condition
Exception

The result is unsuccessful.

View Source

ToResult()

Converts this result into Result<T>.

Declaration
public Result<T> ToResult()
Returns
Type Description
Result<T>

The converted result.

View Source

ToString()

Returns textual representation of this object.

Declaration
public override string? ToString()
Returns
Type Description
string

The textual representation of this object.

Overrides
ValueType.ToString()
View Source

TryGet()

Converts the result into Optional<T>.

Declaration
public Optional<T> TryGet()
Returns
Type Description
Optional<T>

Option monad representing value in this monad.

View Source

TryGet(out T)

Attempts to extract value from the container if it is present.

Declaration
public bool TryGet(out T result)
Parameters
Type Name Description
T result

Extracted value.

Returns
Type Description
bool

true if value is present; otherwise, false.

Operators

View Source

operator &(in Result<T, TError>, in Result<T, TError>)

Indicates that both results are successful.

Declaration
public static bool operator &(in Result<T, TError> left, in Result<T, TError> right)
Parameters
Type Name Description
Result<T, TError> left

The first result to check.

Result<T, TError> right

The second result to check.

Returns
Type Description
bool

true if both results are successful; otherwise, false.

View Source

operator |(in Result<T, TError>, in Result<T, TError>)

Tries to return successful result.

Declaration
public static Result<T, TError> operator |(in Result<T, TError> x, in Result<T, TError> y)
Parameters
Type Name Description
Result<T, TError> x

The first container.

Result<T, TError> y

The second container.

Returns
Type Description
Result<T, TError>

The first successful result.

View Source

operator |(in Result<T, TError>, T?)

Returns the value if present; otherwise return default value.

Declaration
public static T? operator |(in Result<T, TError> result, T? defaultValue)
Parameters
Type Name Description
Result<T, TError> result

The result to check.

T defaultValue

The value to be returned if this result is unsuccessful.

Returns
Type Description
T

The value, if present, otherwise defaultValue.

View Source

explicit operator T(in Result<T, TError>)

Extracts actual result.

Declaration
public static explicit operator T(in Result<T, TError> result)
Parameters
Type Name Description
Result<T, TError> result

The result object.

Returns
Type Description
T
View Source

operator false(in Result<T, TError>)

Checks whether the container has no value.

Declaration
public static bool operator false(in Result<T, TError> result)
Parameters
Type Name Description
Result<T, TError> result
Returns
Type Description
bool

true if this container has no value; otherwise, false.

View Source

implicit operator Optional<T>(in Result<T, TError>)

Converts the result into Optional<T>.

Declaration
public static implicit operator Optional<T>(in Result<T, TError> result)
Parameters
Type Name Description
Result<T, TError> result

The result to be converted.

Returns
Type Description
Optional<T>

Option monad representing value in this monad.

View Source

implicit operator Result<T>(in Result<T, TError>)

Converts the result into Result<T>.

Declaration
public static implicit operator Result<T>(in Result<T, TError> result)
Parameters
Type Name Description
Result<T, TError> result

The result to be converted.

Returns
Type Description
Result<T>

The converted result.

View Source

operator !(in Result<T, TError>)

Checks whether the container has no value.

Declaration
public static bool operator !(in Result<T, TError> result)
Parameters
Type Name Description
Result<T, TError> result
Returns
Type Description
bool

true if this container has no value; otherwise, false.

View Source

operator true(in Result<T, TError>)

Checks whether the container has value.

Declaration
public static bool operator true(in Result<T, TError> result)
Parameters
Type Name Description
Result<T, TError> result
Returns
Type Description
bool

true if this container has value; otherwise, false.

Explicit Interface Implementations

View Source

implicit operator Result<T, TError>(T)

Converts value into the result.

Declaration
static implicit operator Result<T, TError>(T result)
Parameters
Type Name Description
T result

The result to be converted.

Returns
Type Description
Result<T, TError>

The result representing result value.

Implements

IResultMonad<T, TError, TSelf>
IResultMonad<T, TError>
IOptionMonad<T, TSelf>
IOptionMonad<T>
ISupplier<TResult>
IFunctional

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, params ReadOnlySpan<T>)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾