Class Optional
Various extension and factory methods for constructing optional value.
Namespace: DotNext
Assembly: DotNext.dll
Syntax
public static class Optional : object
Methods
| Improve this Doc View SourceCoalesce<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. |
Convert<I, O>(Task<Optional<I>>, Converter<I, O>)
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 Empty.
Declaration
public static Task<Optional<O>> Convert<I, O>(this Task<Optional<I>> task, Converter<I, O> converter)
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<I>> | task | The task containing Optional value. |
DotNext.Converter<I, O> | converter | A mapping function to be applied to the value, if present. |
Returns
Type | Description |
---|---|
Task<Optional<O>> | An Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise Empty. |
Type Parameters
Name | Description |
---|---|
I | The type of stored in the Optional container. |
O | The type of the result of the mapping function. |
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. |
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. |
DotNext.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. |
IsOptional(Type)
Indicates that specified type is optional type.
Declaration
public static bool IsOptional(this Type optionalType)
Parameters
Type | Name | Description |
---|---|---|
Type | optionalType |
Returns
Type | Description |
---|---|
Boolean | true, if specified type is optional type; otherwise, false. |
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. |
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. |
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. |
DotNext.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. |
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. |
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. |
OrThrow<T, E>(Task<Optional<T>>)
If a value is present, returns the value, otherwise throw exception.
Declaration
public static Task<T> OrThrow<T, E>(this Task<Optional<T>> task)
where E : 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. |
E | Type of exception to throw. |
OrThrow<T, E>(Task<Optional<T>>, Func<E>)
If a value is present, returns the value, otherwise throw exception.
Declaration
public static Task<T> OrThrow<T, E>(this Task<Optional<T>> task, Func<E> exceptionFactory)
where E : Exception
Parameters
Type | Name | Description |
---|---|---|
Task<Optional<T>> | task | The task returning optional value. |
DotNext.Func<E> | exceptionFactory | Exception factory. |
Returns
Type | Description |
---|---|
Task<T> | The value, if present. |
Type Parameters
Name | Description |
---|---|
T | Type of the value. |
E | Type of exception to throw. |
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. |