Table of Contents

Class OptionExtensions

Namespace
DynamicData.Kernel
Assembly
DynamicData.dll

Extensions for optional.

public static class OptionExtensions
Inheritance
OptionExtensions

Methods

ConvertOr<TSource, TDestination>(in Optional<TSource>, Func<TSource?, TDestination?>, Func<TDestination?>)

Converts the option value if it has a value, otherwise returns the result of the fallback converter.

public static TDestination? ConvertOr<TSource, TDestination>(this in Optional<TSource> source, Func<TSource?, TDestination?> converter, Func<TDestination?> fallbackConverter) where TSource : notnull

Parameters

source Optional<TSource>

The source.

converter Func<TSource, TDestination>

The converter.

fallbackConverter Func<TDestination>

The fallback converter.

Returns

TDestination

The destination value.

Type Parameters

TSource

The type of the source.

TDestination

The type of the destination.

Exceptions

ArgumentNullException

converter or fallbackConverter.

Convert<TSource, TDestination>(in Optional<TSource>, Func<TSource, Optional<TDestination>>)

Attempts to converts the specified source, but the conversion might result in a None value.

public static Optional<TDestination> Convert<TSource, TDestination>(this in Optional<TSource> source, Func<TSource, Optional<TDestination>> converter) where TSource : notnull where TDestination : notnull

Parameters

source Optional<TSource>

The source.

converter Func<TSource, Optional<TDestination>>

The converter that returns an optional value.

Returns

Optional<TDestination>

The converted value.

Type Parameters

TSource

The type of the source.

TDestination

The type of the destination.

Exceptions

ArgumentNullException

converter.

Convert<TSource, TDestination>(in Optional<TSource>, Func<TSource, TDestination>)

Converts the specified source.

public static Optional<TDestination> Convert<TSource, TDestination>(this in Optional<TSource> source, Func<TSource, TDestination> converter) where TSource : notnull where TDestination : notnull

Parameters

source Optional<TSource>

The source.

converter Func<TSource, TDestination>

The converter.

Returns

Optional<TDestination>

The converted value.

Type Parameters

TSource

The type of the source.

TDestination

The type of the destination.

Exceptions

ArgumentNullException

converter.

FirstOrOptional<T>(IEnumerable<T>, Func<T, bool>)

Overloads Enumerable.FirstOrDefault() and wraps the result in a Optional &gt;T container.

public static Optional<T> FirstOrOptional<T>(this IEnumerable<T> source, Func<T, bool> selector) where T : notnull

Parameters

source IEnumerable<T>

The source.

selector Func<T, bool>

The selector.

Returns

Optional<T>

The first value or none.

Type Parameters

T

The type of the item.

IfHasValue<T>(in Optional<T>, Action<T>)

Invokes the specified action when.

public static OptionElse IfHasValue<T>(this in Optional<T> source, Action<T> action) where T : notnull

Parameters

source Optional<T>

The source.

action Action<T>

The action.

Returns

OptionElse

The optional else extension.

Type Parameters

T

The type of the item.

IfHasValue<T>(Optional<T>?, Action<T>)

Invokes the specified action when.

public static OptionElse IfHasValue<T>(this Optional<T>? source, Action<T> action) where T : notnull

Parameters

source Optional<T>?

The source.

action Action<T>

The action.

Returns

OptionElse

The optional else extension.

Type Parameters

T

The type of the item.

Lookup<TValue, TKey>(IDictionary<TKey, TValue>, TKey)

Overloads a TryGetValue of the dictionary wrapping the result as an Optional. &gt;TValue

public static Optional<TValue> Lookup<TValue, TKey>(this IDictionary<TKey, TValue> source, TKey key) where TValue : notnull

Parameters

source IDictionary<TKey, TValue>

The source.

key TKey

The key.

Returns

Optional<TValue>

The option of the looked up value.

Type Parameters

TValue

The type of the value.

TKey

The type of the key.

OrElse<T>(in Optional<T>, Func<Optional<T>>)

Returns the original optional if it has a value, otherwise returns the result of the fallback operation.

public static Optional<T> OrElse<T>(this in Optional<T> source, Func<Optional<T>> fallbackOperation) where T : notnull

Parameters

source Optional<T>

The source.

fallbackOperation Func<Optional<T>>

The fallback operation.

Returns

Optional<T>

The original value or the result of the fallback operation.

Type Parameters

T

The type of the source.

Exceptions

ArgumentNullException

converter or fallbackOperation.

RemoveIfContained<TValue, TKey>(IDictionary<TKey, TValue>, TKey)

Removes item if contained in the cache.

public static bool RemoveIfContained<TValue, TKey>(this IDictionary<TKey, TValue> source, TKey key)

Parameters

source IDictionary<TKey, TValue>

The source.

key TKey

The key.

Returns

bool

If the item was removed.

Type Parameters

TValue

The type of the value.

TKey

The type of the key.

SelectValues<T>(IEnumerable<Optional<T>>)

Filters where Optional has a value and return the values only.

public static IEnumerable<T> SelectValues<T>(this IEnumerable<Optional<T>> source) where T : notnull

Parameters

source IEnumerable<Optional<T>>

The source.

Returns

IEnumerable<T>

An enumerable of the selected items.

Type Parameters

T

The type of item.

ValueOrDefault<T>(in Optional<T>)

Returns the value if the optional has a value, otherwise returns the default value of T.

public static T? ValueOrDefault<T>(this in Optional<T> source) where T : notnull

Parameters

source Optional<T>

The source.

Returns

T

The value or default.

Type Parameters

T

The type of the item.

ValueOrThrow<T>(in Optional<T>, Func<Exception>)

Returns the value if the optional has a value, otherwise throws an exception as specified by the exception generator.

public static T ValueOrThrow<T>(this in Optional<T> source, Func<Exception> exceptionGenerator) where T : notnull

Parameters

source Optional<T>

The source.

exceptionGenerator Func<Exception>

The exception generator.

Returns

T

The value.

Type Parameters

T

The type of the item.

Exceptions

ArgumentNullException

exceptionGenerator.

ValueOr<T>(in Optional<T>, Func<T>)

Returns the value if the optional has a value, otherwise returns the result of the value selector.

public static T ValueOr<T>(this in Optional<T> source, Func<T> valueSelector) where T : notnull

Parameters

source Optional<T>

The source.

valueSelector Func<T>

The value selector.

Returns

T

If the value or a provided default.

Type Parameters

T

The type of the item.

Exceptions

ArgumentNullException

valueSelector.

ValueOr<T>(T?, T)

Returns the value if the nullable has a value, otherwise returns the result of the value selector.

public static T ValueOr<T>(this T? source, T defaultValue) where T : struct

Parameters

source T?

The source.

defaultValue T

The default value.

Returns

T

The value or the default value.

Type Parameters

T

The type of the item.