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

| Improve this Doc View Source

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

Returns the second value if the first is empty.

Declaration
public static 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.

| Improve this Doc 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 result of the mapping function.

| Improve this Doc 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 optional type; otherwise, null.

| Improve this Doc 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.

| Improve this Doc 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
Boolean

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

| Improve this Doc 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.

| Improve this Doc 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.

| Improve this Doc 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.

| Improve this Doc 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, if present, otherwise default.

Type Parameters
Name Description
T

Type of the value.

| Improve this Doc 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.

| Improve this Doc View Source

OrNull<T>(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
Nullable<T>

Nullable value.

Type Parameters
Name Description
T

Value type.

| Improve this Doc 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<Nullable<T>>

Nullable value.

Type Parameters
Name Description
T

The type of the value.

| Improve this Doc 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.

| Improve this Doc 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.

| Improve this Doc 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.

| Improve this Doc View Source

ToOptional<T>(Nullable<T>)

Constructs optional value from nullable reference type.

Declaration
public static Optional<T> ToOptional<T>(this in T? value)

    where T : struct
Parameters
Type Name Description
Nullable<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.

  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX