Show / Hide Table of Contents

Class Optional

Various extension and factory methods for constructing optional value.

Inheritance
object
Optional
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext
Assembly: DotNext.dll
Syntax
public static class Optional

Methods

| Edit this page View Source

Coalesce<T>(in Optional<T>, in Optional<T>)

Returns the second value if the first is empty.

Declaration
public static ref readonly Optional<T> Coalesce<T>(this in Optional<T> first, in Optional<T> second)
Parameters
Type Name Description
Optional<T> first

The first optional value.

Optional<T> second

The second optional value.

Returns
Type Description
Optional<T>

The second value if the first is empty; otherwise, the first value.

Type Parameters
Name Description
T

Type of value.

| Edit this page View Source

Convert<TInput, TOutput>(Task<Optional<TInput>>, Converter<TInput, TOutput>)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise, returns None.

Declaration
public static Task<Optional<TOutput>> Convert<TInput, TOutput>(this Task<Optional<TInput>> task, Converter<TInput, TOutput> converter)
Parameters
Type Name Description
Task<Optional<TInput>> task

The task containing Optional value.

Converter<TInput, TOutput> converter

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

Returns
Type Description
Task<Optional<TOutput>>

An Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise None.

Type Parameters
Name Description
TInput

The type of stored in the Optional container.

TOutput

The type of the mapping function result.

| Edit this page View Source

Convert<TInput, TOutput>(Task<Optional<TInput>>, Func<TInput, CancellationToken, Task<TOutput>>, CancellationToken)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise, returns None.

Declaration
public static Task<Optional<TOutput>> Convert<TInput, TOutput>(this Task<Optional<TInput>> task, Func<TInput, CancellationToken, Task<TOutput>> converter, CancellationToken token = default)
Parameters
Type Name Description
Task<Optional<TInput>> task

The task containing Optional value.

Func<TInput, CancellationToken, Task<TOutput>> converter

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

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
Task<Optional<TOutput>>

An Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise None.

Type Parameters
Name Description
TInput

The type of stored in the Optional container.

TOutput

The type of the mapping function result.

| Edit this page View Source

Flatten<T>(in Optional<Optional<T>>)

Flattens the nested optional value.

Declaration
public static Optional<T> Flatten<T>(this in Optional<Optional<T>> optional)
Parameters
Type Name Description
Optional<Optional<T>> optional

The nested optional value.

Returns
Type Description
Optional<T>

Flattened value.

Type Parameters
Name Description
T

The type of the underlying value.

| Edit this page View Source

Flatten<T>(Task<Optional<T>>)

Returns a task that contains unwrapped value; or exception if Optional<T> has no value.

Declaration
public static Task<T> Flatten<T>(this Task<Optional<T>> task)
Parameters
Type Name Description
Task<Optional<T>> task

The task representing optional value.

Returns
Type Description
Task<T>

The task containing a value of type T; or the exception if Optional<T> has no value.

Type Parameters
Name Description
T

The type of the value.

Exceptions
Type Condition
InvalidOperationException

No value is present.

| Edit this page View Source

GetUnderlyingType(Type)

Returns the underlying type argument of the specified optional type.

Declaration
public static Type? GetUnderlyingType(Type optionalType)
Parameters
Type Name Description
Type optionalType

Optional type.

Returns
Type Description
Type

Underlying type argument of the optional type; otherwise, null.

| Edit this page View Source

If<T>(Task<Optional<T>>, Predicate<T>)

If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.

Declaration
public static Task<Optional<T>> If<T>(this Task<Optional<T>> task, Predicate<T> condition)
Parameters
Type Name Description
Task<Optional<T>> task

The task returning optional value.

Predicate<T> condition

A predicate to apply to the value, if present.

Returns
Type Description
Task<Optional<T>>

An Optional describing the value of this Optional if a value is present and the value matches the given predicate, otherwise an empty Optional.

Type Parameters
Name Description
T

Type of the value.

| Edit this page View Source

IsOptional(Type)

Indicates that specified type is optional type.

Declaration
public static bool IsOptional(this Type optionalType)
Parameters
Type Name Description
Type optionalType

The type to check.

Returns
Type Description
bool

true, if specified type is optional type; otherwise, false.

| Edit this page View Source

None<T>()

Returns empty value.

Declaration
public static Optional<T> None<T>()
Returns
Type Description
Optional<T>

The empty value.

Type Parameters
Name Description
T

The type of empty result.

| Edit this page View Source

Null<T>()

Wraps null value to Optional<T> container.

Declaration
public static Optional<T> Null<T>() where T : class?
Returns
Type Description
Optional<T>

The Optional<T> instance representing null value.

Type Parameters
Name Description
T

The reference type.

| Edit this page View Source

OrDefault<T>(Task<Optional<T>>)

If a value is present, returns the value, otherwise return default value.

Declaration
public static Task<T?> OrDefault<T>(this Task<Optional<T>> task)
Parameters
Type Name Description
Task<Optional<T>> task

The task returning optional value.

Returns
Type Description
Task<T>

The value stored in the container, if present, otherwise default value.

Type Parameters
Name Description
T

Type of the value.

| Edit this page View Source

OrInvoke<T>(Task<Optional<T>>, Func<T>)

Returns the value if present; otherwise invoke delegate.

Declaration
public static Task<T> OrInvoke<T>(this Task<Optional<T>> task, Func<T> defaultFunc)
Parameters
Type Name Description
Task<Optional<T>> task

The task returning optional value.

Func<T> defaultFunc

A delegate to be invoked if value is not present.

Returns
Type Description
Task<T>

The value, if present, otherwise returned from delegate.

Type Parameters
Name Description
T

Type of the value.

| Edit this page View Source

OrNull<T>(in Optional<T>)

If a value is present, returns the value, otherwise null.

Declaration
public static T? OrNull<T>(this in Optional<T> value) where T : struct
Parameters
Type Name Description
Optional<T> value

Optional value.

Returns
Type Description
T?

Nullable value.

Type Parameters
Name Description
T

Value type.

| Edit this page View Source

OrNull<T>(Task<Optional<T>>)

If a value is present, returns the value, otherwise null.

Declaration
public static Task<T?> OrNull<T>(this Task<Optional<T>> task) where T : struct
Parameters
Type Name Description
Task<Optional<T>> task

The task returning optional value.

Returns
Type Description
Task<T?>

Nullable value.

Type Parameters
Name Description
T

The type of the value.

| Edit this page View Source

OrThrow<T, TException>(Task<Optional<T>>)

If a value is present, returns the value, otherwise throw exception.

Declaration
public static Task<T> OrThrow<T, TException>(this Task<Optional<T>> task) where TException : Exception, new()
Parameters
Type Name Description
Task<Optional<T>> task

The task returning optional value.

Returns
Type Description
Task<T>

The value, if present.

Type Parameters
Name Description
T

Type of the value.

TException

Type of exception to throw.

| Edit this page View Source

OrThrow<T, TException>(Task<Optional<T>>, Func<TException>)

If a value is present, returns the value, otherwise throw exception.

Declaration
public static Task<T> OrThrow<T, TException>(this Task<Optional<T>> task, Func<TException> exceptionFactory) where TException : Exception
Parameters
Type Name Description
Task<Optional<T>> task

The task returning optional value.

Func<TException> exceptionFactory

Exception factory.

Returns
Type Description
Task<T>

The value, if present.

Type Parameters
Name Description
T

Type of the value.

TException

Type of exception to throw.

| Edit this page View Source

Or<T>(Task<Optional<T>>, T?)

Returns the value if present; otherwise return default value.

Declaration
public static Task<T?> Or<T>(this Task<Optional<T>> task, T? defaultValue)
Parameters
Type Name Description
Task<Optional<T>> task

The task returning optional value.

T defaultValue

The value to be returned if there is no value present.

Returns
Type Description
Task<T>

The value, if present, otherwise default.

Type Parameters
Name Description
T

The type of the value.

| Edit this page View Source

Some<T>(T)

Wraps the value to Optional<T> container.

Declaration
public static Optional<T> Some<T>(T value)
Parameters
Type Name Description
T value

The value to be wrapped.

Returns
Type Description
Optional<T>

The optional container.

Type Parameters
Name Description
T

The type of the value.

| Edit this page View Source

ToOptional<T>(in T?)

Constructs optional value from nullable value type.

Declaration
public static Optional<T> ToOptional<T>(this in T? value) where T : struct
Parameters
Type Name Description
T? value

The value to convert.

Returns
Type Description
Optional<T>

The value wrapped into Optional container.

Type Parameters
Name Description
T

Type of value.

  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾