Show / Hide Table of Contents

Struct Result<T>

Represents a result of operation which can be the actual result or exception.

Implements
IResultMonad<T, Exception, Result<T>>
IOptionMonad<T, Result<T>>
IResultMonad<T>
IResultMonad<T, Exception>
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> : IResultMonad<T, Exception, Result<T>>, IOptionMonad<T, Result<T>>, IResultMonad<T>, IResultMonad<T, Exception>, IOptionMonad<T>, ISupplier<object?>, IFunctional
Type Parameters
Name Description
T

The type of the value stored in the Result monad.

Constructors

View Source

Result(Exception)

Initializes a new unsuccessful result.

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

The exception representing error. Cannot be null.

View Source

Result(T)

Initializes a new successful result.

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

The value to be stored as result.

Properties

View Source

Error

Gets exception associated with this result.

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

IsSuccessful

Indicates that the result is successful.

Declaration
[MemberNotNullWhen(false, "Error")]
[MemberNotNullWhen(false, "exception")]
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
Exception

This result is not successful.

View Source

ValueOrDefault

Gets 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
Exception

The result is unavailable.

Methods

View Source

AsTask()

Converts this result to Task<TResult>.

Declaration
public ValueTask<T> AsTask()
Returns
Type Description
ValueTask<T>

The completed task representing the result.

View Source

Box()

Gets boxed representation of the result.

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

The boxed representation of the result.

View Source

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

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

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

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

Returns
Type Description
Result<TResult>

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>>)

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

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

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

Returns
Type Description
Result<TResult>

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>>)

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

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

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

Returns
Type Description
Result<TResult>

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> 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>

The conversion result.

Type Parameters
Name Description
TResult

The type of the mapping function result.

View Source

Create<TMonad>(TMonad)

Converts the monad to Result<T>.

Declaration
public static Result<T> Create<TMonad>(TMonad value) where TMonad : struct, IResultMonad<T, Exception>
Parameters
Type Name Description
TMonad value

The value of the monad.

Returns
Type Description
Result<T>

The result constructed from the monad.

Type Parameters
Name Description
TMonad

The type of the monad.

View Source

FromOptional(in Optional<T>)

Creates Result<T> from Optional<T> instance.

Declaration
public static Result<T> FromOptional(in Optional<T> optional)
Parameters
Type Name Description
Optional<T> optional

The optional value.

Returns
Type Description
Result<T>

The converted optional value.

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*<Exception, T>)

Returns the value if present; otherwise invoke delegate.

Declaration
[CLSCompliant(false)]
public T OrInvoke(delegate*<Exception, T> defaultFunc)
Parameters
Type Name Description
delegate*<Exception, 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*<Exception, T>)

Returns the value if present; otherwise invoke delegate.

Declaration
[CLSCompliant(false)]
public T OrInvoke(delegate*<Exception, T> defaultFunc)
Parameters
Type Name Description
delegate*<Exception, 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<Exception, T>)

Returns the value if present; otherwise invoke delegate.

Declaration
public T OrInvoke(Func<Exception, T> defaultFunc)
Parameters
Type Name Description
Func<Exception, 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

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>, in Result<T>)

Indicates that both results are successful.

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

The first result to check.

Result<T> right

The second result to check.

Returns
Type Description
bool

true if both results are successful; otherwise, false.

View Source

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

Tries to return successful result.

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

The first container.

Result<T> y

The second container.

Returns
Type Description
Result<T>

The first successful result.

View Source

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

Returns the value if present; otherwise return default value.

Declaration
public static T? operator |(in Result<T> result, T? defaultValue)
Parameters
Type Name Description
Result<T> 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 Result<T>(in Optional<T>)

Converts Optional<T> to Result<T>.

Declaration
public static explicit operator Result<T>(in Optional<T> optional)
Parameters
Type Name Description
Optional<T> optional

The optional value.

Returns
Type Description
Result<T>

The result representing optional value.

View Source

explicit operator ValueTask<T>(in Result<T>)

Converts the result to Task<TResult>.

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

The result to be converted.

Returns
Type Description
ValueTask<T>

The completed task representing the result.

View Source

explicit operator T(in Result<T>)

Extracts actual result.

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

The result object.

Returns
Type Description
T
View Source

operator false(in Result<T>)

Checks whether the container has no value.

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

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

View Source

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

Converts the result into Optional<T>.

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

The result to be converted.

Returns
Type Description
Optional<T>

Option monad representing value in this monad.

View Source

operator !(in Result<T>)

Checks whether the container has no value.

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

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

View Source

operator true(in Result<T>)

Checks whether the container has value.

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

true if this container has value; otherwise, false.

Explicit Interface Implementations

View Source

implicit operator Result<T>(T)

Converts value into the result.

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

The result to be converted.

Returns
Type Description
Result<T>

The result representing result value.

Implements

IResultMonad<T, TError, TSelf>
IOptionMonad<T, TSelf>
IResultMonad<T>
IResultMonad<T, TError>
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)
Result.Coalesce<T>(in Result<T>, in Result<T>)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾