Repository navigation
OnixLabs.Core.Optional
Represents an optional value — a functional Optional (also known as Maybe or Option) type that makes the presence or absence of a value explicit in the type system, as an alternative to using null.
| Namespace | OnixLabs.Core |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → Optional<T>
|
| Implements |
IValueEquatable<Optional<T>> (and therefore IEquatable<Optional<T>>) |
| Derived |
Some<T>, None<T>
|
public closed class Optional<T> : IValueEquatable<Optional<T>>
where T : notnull| Parameter | Description |
|---|---|
T |
The type of the underlying optional value. Constrained to notnull, so Optional<T> always describes a value type or a non-nullable reference type. |
Optional<T> models the Optional / Maybe pattern as a closed (discriminated) hierarchy with exactly two cases:
-
Some<T>— a present value, holding the underlyingTin itsValueproperty. -
None<T>— an absent value, carrying no data.
Optional<T> is a C# 15 closed class: it cannot be instantiated directly, its constructor is private protected, and both cases are sealed, so the only instances that can exist are Some<T> and None<T>. This lets you pattern-match exhaustively over the two cases, or use the Match, Select, and SelectMany methods to operate on the value without ever touching null.
Because the hierarchy is closed, the compiler knows that Some<T> and None<T> are the only cases. With C# 15, a switch over them is exhaustive without a discard arm:
string message = optional switch
{
Some<int> some => $"Value: {some.Value}",
None<int> => "No value"
};This works on every target framework, including .NET 8.0. Consumers compiling with an earlier language version see no change and can keep their existing discard arms.
There are several ways to construct an Optional<T>, and they differ in how they treat default values:
-
Some(T)wraps any non-nullvalue, including value-type defaults such as0orGuid.Empty. Passingnullthrows. - The implicit conversion from
Tbehaves likeSomefor non-nullvalues and yieldsNonefornull. Value-type defaults are preserved asSome. -
Of(T?)performs strip-default wrapping: anynullordefaultvalue collapses toNone, soOf(0),Of(Guid.Empty), andOf(default)all returnNone. -
Of<TStruct>(TStruct?)lifts aNullable<TStruct>into anOptional<TStruct>, mappingnulltoNoneand a present value toSome.
Note Choose the construction style by intent. Prefer
Of(T?)when value-type defaults should be treated as absent (for example, dictionary-lookup or struct-default APIs). PreferSomeor the implicit conversion when onlynullshould be treated as absent.
Equality is value-based and delegates to OptionalEqualityComparer<T>: two None instances are equal, a Some is never equal to a None, and two Some instances are equal when their underlying values are equal. GetHashCode() returns the hash of the underlying value for Some, and 0 for None. ToString() returns the underlying value's string representation for Some, and "None" for None.
Asynchronous counterparts of Match, Select, and SelectMany are declared directly on Optional<T>: MatchAsync, SelectAsync, and SelectManyAsync. Each accepts Task-returning or ValueTask-returning delegates (with or without a CancellationToken parameter) and a trailing CancellationToken cancellationToken = default; overloads taking ValueTask-returning delegates return ValueTask/ValueTask<…>. Further overloads over Task<Optional<T>> and ValueTask<Optional<T>> receivers are provided by OptionalExtensions.
Overload resolution note
MatchAsync,SelectAsync, andSelectManyAsynchave bothTask-returning andValueTask-returning delegate overloads. Anasynclambda (or a lambda whose body is only athrowexpression) is convertible to both delegate types, so such a call is ambiguous and fails with compiler error CS0121. Give the lambda an explicit return type to select an overload, for exampleasync Task<Optional<User>> (id) => ...orasync ValueTask (value) => ....
The concrete cases are documented on their own pages:
-
Some<T>— the present case. -
None<T>— the absent case. -
OptionalEqualityComparer<T>— the equality comparer used byOptional<T>.Equalsand the equality operators.
using OnixLabs.Core;
Optional<int> some = Optional<int>.Some(42); // Some(42)
Optional<int> none = Optional<int>.None; // None
Optional<string> implicitSome = "hello"; // Some("hello") via implicit conversion
Optional<string> implicitNone = null; // None via implicit conversion
// Strip-default wrapping: value-type defaults collapse to None.
Optional<int> stripped = Optional<int>.Of(0); // None
Optional<int> kept = 0; // Some(0) via implicit conversion
// Lifting a Nullable<T>.
int? maybe = 10;
Optional<int> lifted = Optional<int>.Of(maybe); // Some(10)Optional<int> optional = Optional<int>.Some(21);
// Branch on presence/absence and produce a result.
string message = optional.Match(
some: value => $"The value is {value}.",
none: () => "There is no value.");
// Transform the underlying value, propagating None unchanged.
Optional<string> mapped = optional.Select(value => (value * 2).ToString()); // Some("42")
// Chain operations that themselves return Optional<T>.
Optional<int> chained = optional.SelectMany(value => Optional<int>.Of(value - 21)); // None (Of(0) is None)Optional<string> optional = "value";
if (optional.HasValue)
{
string value = optional.GetValueOrThrow();
}
string orDefault = Optional<string>.None.GetValueOrDefault("fallback"); // "fallback"| Field | Type | Description |
|---|---|---|
None |
None<T> |
The shared singleton representing an absent optional value. |
public static readonly None<T> NoneThe shared None<T> singleton instance representing an absent optional value for the type T.
| Property | Type | Description |
|---|---|---|
HasValue |
bool |
Gets a value indicating whether the underlying value is present. |
public bool HasValue { get; }Gets a value indicating whether the underlying value of the current Optional<T> instance is present.
Returns — true if the current instance is a Some<T>; otherwise, false.
| Method | Returns | Description |
|---|---|---|
IsNone(Optional<T>) |
bool |
Determines whether the specified instance is None<T>. |
IsSome(Optional<T>) |
bool |
Determines whether the specified instance is Some<T>. |
Of(T?) |
Optional<T> |
Creates an optional value, treating null/default as absent. |
Of<TStruct>(TStruct?) |
Optional<TStruct> |
Lifts a Nullable<TStruct> into an optional value. |
Some(T) |
Some<T> |
Creates a present optional value from a non-null value. |
| Method | Returns | Description |
|---|---|---|
Equals(Optional<T>?) |
bool |
Determines whether this instance equals another optional value. |
Equals(object?) |
bool |
Determines whether this instance equals another object. |
GetHashCode() |
int |
Returns a hash code derived from the underlying value. |
GetValueOrDefault() |
T? |
Gets the underlying value, or the default of T if absent. |
GetValueOrDefault(T) |
T |
Gets the underlying value, or a specified default if absent. |
GetValueOrThrow() |
T |
Gets the underlying value, or throws if absent. |
Match(Action<T>, Action) |
void |
Executes the action matching the present/absent case. |
Match<TResult>(Func<T, TResult>, Func<TResult>) |
TResult |
Executes the function matching the case and returns its result. |
MatchAsync(...) |
Task / Task<TResult> / ValueTask / ValueTask<TResult>
|
Asynchronously executes the delegate matching the present/absent case. Overloads accept synchronous, Task-returning, or ValueTask-returning some/none delegates. |
Select<TResult>(Func<T, TResult>) |
Optional<TResult> |
Projects the underlying value into a new optional value. |
SelectAsync(...) |
Task<Optional<TResult>> / ValueTask<Optional<TResult>>
|
Asynchronously projects the underlying value into a new optional value. |
SelectMany<TResult>(Func<T, Optional<TResult>>) |
Optional<TResult> |
Projects and flattens the underlying value into a new optional value. |
SelectManyAsync(...) |
Task<Optional<TResult>> / ValueTask<Optional<TResult>>
|
Asynchronously projects and flattens the underlying value into a new optional value. |
ToResult() |
Result<Optional<T>> |
Wraps this instance into a successful result. |
ToString() |
string |
Returns the string representation of the underlying value, or "None". |
public static bool IsNone(Optional<T> value)Determines whether the specified Optional<T> instance is None<T>.
| Parameter | Type | Description |
|---|---|---|
value |
Optional<T> |
The optional value to check. |
Returns — true if value is a None<T>; otherwise, false.
public static bool IsSome(Optional<T> value)Determines whether the specified Optional<T> instance is Some<T>.
| Parameter | Type | Description |
|---|---|---|
value |
Optional<T> |
The optional value to check. |
Returns — true if value is a Some<T>; otherwise, false.
public static Optional<T> Of(T? value)Creates a new Optional<T> instance using strip-default wrapping: the underlying value is present only if value is neither null nor equal to default(T) (compared with EqualityComparer<T>.Default); otherwise the result is None.
| Parameter | Type | Description |
|---|---|---|
value |
T? |
The underlying optional value. |
Returns — Some wrapping value if it is non-null and not the default of T; otherwise None.
public static Optional<TStruct> Of<TStruct>(TStruct? value)
where TStruct : structCreates a new Optional<TStruct> instance from a Nullable<TStruct>, where the underlying value is present if value is not null; otherwise the result is None.
| Parameter | Type | Description |
|---|---|---|
value |
TStruct? |
The nullable value type to lift into an optional value. |
| Type Parameter | Description |
|---|---|
TStruct |
The underlying value type of the optional value. Constrained to struct. |
Returns — Optional<TStruct>.Some(value.Value) if value has a value; otherwise Optional<TStruct>.None.
public static Some<T> Some(T value)Creates a new Some<T> instance wrapping the specified non-null value. Unlike Of(T?), value-type defaults (for example 0) are preserved as Some.
| Parameter | Type | Description |
|---|---|---|
value |
T |
The underlying optional value. Must not be null. |
Returns — A Some<T> instance holding value.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
value is null. |
public bool Equals(Optional<T>? other)Determines whether the current instance is equal to another Optional<T> instance, delegating to OptionalEqualityComparer<T>.Default. Two None instances are equal; a Some and a None are not equal; two Some instances are equal when their underlying values are equal.
Returns — true if the instances are equal; otherwise, false.
public sealed override bool Equals(object? obj)Determines whether the current instance is equal to another object. Behaves as Equals(Optional<T>?) when obj is an Optional<T>.
Returns — true if obj is an equal Optional<T>; otherwise, false.
public sealed override int GetHashCode()Returns a hash code for the current instance. For a Some<T>, returns the hash code of the underlying value; for a None<T>, returns 0.
Returns — A 32-bit signed integer hash code.
public T? GetValueOrDefault()Gets the underlying value if present; otherwise returns the default value of T.
Returns — The underlying value if the current instance is a Some<T>; otherwise default(T).
public T GetValueOrDefault(T defaultValue)Gets the underlying value if present; otherwise returns the specified default value.
| Parameter | Type | Description |
|---|---|---|
defaultValue |
T |
The default value to return when the underlying value is absent. |
Returns — The underlying value if the current instance is a Some<T>; otherwise defaultValue.
public T GetValueOrThrow()Gets the underlying value if present; otherwise throws.
Returns — The underlying value held by the current Some<T>.
Exceptions
| Exception | Condition |
|---|---|
InvalidOperationException |
The underlying value is absent (the current instance is a None<T>). |
public void Match(Action<T>? some = null, Action? none = null)Executes the action that matches the current case. If the value is present and some is non-null, some is invoked with the underlying value; otherwise, if none is non-null, none is invoked.
| Parameter | Type | Description |
|---|---|---|
some |
Action<T>? |
The action to execute when the underlying value is present. Optional. |
none |
Action? |
The action to execute when the underlying value is absent. Optional. |
public TResult Match<TResult>(Func<T, TResult> some, Func<TResult> none)Executes the function that matches the current case and returns its result.
| Parameter | Type | Description |
|---|---|---|
some |
Func<T, TResult> |
The function to execute when the underlying value is present. |
none |
Func<TResult> |
The function to execute when the underlying value is absent. |
| Type Parameter | Description |
|---|---|
TResult |
The type of the result produced by the matching function. |
Returns — The result of some if the value is present; otherwise the result of none.
Asynchronously executes the delegate that matches the current Optional<T>: some receives the underlying value when present; otherwise none is invoked. Overloads cover combinations of synchronous, Task-returning, and ValueTask-returning some/none delegates (at least one asynchronous), with and without a CancellationToken parameter, for both the void and value-returning (<TResult>) forms. Both delegates are optional in the void forms.
| Overload | Returns | Delegates |
|---|---|---|
MatchAsync(Func<T, Task>?, Action?, CancellationToken) |
Task |
some: Task-returning; none: synchronous |
MatchAsync(Func<T, ValueTask>?, Action?, CancellationToken) |
ValueTask |
some: ValueTask-returning; none: synchronous |
MatchAsync(Func<T, CancellationToken, Task>?, Action?, CancellationToken) |
Task |
some: Task-returning (cancellable); none: synchronous |
MatchAsync(Func<T, CancellationToken, ValueTask>?, Action?, CancellationToken) |
ValueTask |
some: ValueTask-returning (cancellable); none: synchronous |
MatchAsync(Action<T>?, Func<Task>?, CancellationToken) |
Task |
some: synchronous; none: Task-returning |
MatchAsync(Action<T>?, Func<ValueTask>?, CancellationToken) |
ValueTask |
some: synchronous; none: ValueTask-returning |
MatchAsync(Action<T>?, Func<CancellationToken, Task>?, CancellationToken) |
Task |
some: synchronous; none: Task-returning (cancellable) |
MatchAsync(Action<T>?, Func<CancellationToken, ValueTask>?, CancellationToken) |
ValueTask |
some: synchronous; none: ValueTask-returning (cancellable) |
MatchAsync(Func<T, Task>?, Func<Task>?, CancellationToken) |
Task |
some: Task-returning; none: Task-returning |
MatchAsync(Func<T, ValueTask>?, Func<ValueTask>?, CancellationToken) |
ValueTask |
some: ValueTask-returning; none: ValueTask-returning |
MatchAsync(Func<T, CancellationToken, Task>?, Func<CancellationToken, Task>?, CancellationToken) |
Task |
some: Task-returning (cancellable); none: Task-returning (cancellable) |
MatchAsync(Func<T, CancellationToken, ValueTask>?, Func<CancellationToken, ValueTask>?, CancellationToken) |
ValueTask |
some: ValueTask-returning (cancellable); none: ValueTask-returning (cancellable) |
MatchAsync<TResult>(Func<T, Task<TResult>>, Func<TResult>, CancellationToken) |
Task<TResult> |
some: Task-returning; none: synchronous |
MatchAsync<TResult>(Func<T, ValueTask<TResult>>, Func<TResult>, CancellationToken) |
ValueTask<TResult> |
some: ValueTask-returning; none: synchronous |
MatchAsync<TResult>(Func<T, CancellationToken, Task<TResult>>, Func<TResult>, CancellationToken) |
Task<TResult> |
some: Task-returning (cancellable); none: synchronous |
MatchAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>>, Func<TResult>, CancellationToken) |
ValueTask<TResult> |
some: ValueTask-returning (cancellable); none: synchronous |
MatchAsync<TResult>(Func<T, TResult>, Func<Task<TResult>>, CancellationToken) |
Task<TResult> |
some: synchronous; none: Task-returning |
MatchAsync<TResult>(Func<T, TResult>, Func<ValueTask<TResult>>, CancellationToken) |
ValueTask<TResult> |
some: synchronous; none: ValueTask-returning |
MatchAsync<TResult>(Func<T, TResult>, Func<CancellationToken, Task<TResult>>, CancellationToken) |
Task<TResult> |
some: synchronous; none: Task-returning (cancellable) |
MatchAsync<TResult>(Func<T, TResult>, Func<CancellationToken, ValueTask<TResult>>, CancellationToken) |
ValueTask<TResult> |
some: synchronous; none: ValueTask-returning (cancellable) |
MatchAsync<TResult>(Func<T, Task<TResult>>, Func<Task<TResult>>, CancellationToken) |
Task<TResult> |
some: Task-returning; none: Task-returning |
MatchAsync<TResult>(Func<T, ValueTask<TResult>>, Func<ValueTask<TResult>>, CancellationToken) |
ValueTask<TResult> |
some: ValueTask-returning; none: ValueTask-returning |
MatchAsync<TResult>(Func<T, CancellationToken, Task<TResult>>, Func<CancellationToken, Task<TResult>>, CancellationToken) |
Task<TResult> |
some: Task-returning (cancellable); none: Task-returning (cancellable) |
MatchAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>>, Func<CancellationToken, ValueTask<TResult>>, CancellationToken) |
ValueTask<TResult> |
some: ValueTask-returning (cancellable); none: ValueTask-returning (cancellable) |
public async Task MatchAsync(Func<T, Task>? some = null, Action? none = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<T, ValueTask>? some = null, Action? none = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Func<T, CancellationToken, Task>? some = null, Action? none = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<T, CancellationToken, ValueTask>? some = null, Action? none = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Action<T>? some = null, Func<Task>? none = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Action<T>? some = null, Func<ValueTask>? none = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Action<T>? some = null, Func<CancellationToken, Task>? none = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Action<T>? some = null, Func<CancellationToken, ValueTask>? none = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Func<T, Task>? some = null, Func<Task>? none = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<T, ValueTask>? some = null, Func<ValueTask>? none = null, CancellationToken cancellationToken = default)
public async Task MatchAsync(Func<T, CancellationToken, Task>? some = null, Func<CancellationToken, Task>? none = null, CancellationToken cancellationToken = default)
public async ValueTask MatchAsync(Func<T, CancellationToken, ValueTask>? some = null, Func<CancellationToken, ValueTask>? none = null, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, Task<TResult>> some, Func<TResult> none, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, ValueTask<TResult>> some, Func<TResult> none, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, CancellationToken, Task<TResult>> some, Func<TResult> none, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>> some, Func<TResult> none, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, TResult> some, Func<Task<TResult>> none, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, TResult> some, Func<ValueTask<TResult>> none, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, TResult> some, Func<CancellationToken, Task<TResult>> none, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, TResult> some, Func<CancellationToken, ValueTask<TResult>> none, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, Task<TResult>> some, Func<Task<TResult>> none, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, ValueTask<TResult>> some, Func<ValueTask<TResult>> none, CancellationToken cancellationToken = default)
public async Task<TResult> MatchAsync<TResult>(Func<T, CancellationToken, Task<TResult>> some, Func<CancellationToken, Task<TResult>> none, CancellationToken cancellationToken = default)
public async ValueTask<TResult> MatchAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>> some, Func<CancellationToken, ValueTask<TResult>> none, CancellationToken cancellationToken = default)Parameters
| Parameter | Description |
|---|---|
some |
The delegate to execute when the underlying value of the current Optional<T> instance is present. |
none |
The delegate to execute when the underlying value of the current Optional<T> instance is absent. |
cancellationToken |
The token to monitor for cancellation requests. |
Type Parameters
| Type Parameter | Description |
|---|---|
TResult |
The underlying type of the result produced by the matching delegate. |
Returns
-
Task/ValueTask— ATaskorValueTask(matching the overload) that represents the asynchronous operation. -
Task<TResult>/ValueTask<TResult>— The result of thesomedelegate if the underlying value of the currentOptional<T>instance is present; otherwise, returns the result of thenonedelegate if the underlying value of the currentOptional<T>instance is absent.
Every overload that takes a Task-returning delegate has a twin that takes the equivalent ValueTask-returning delegate and returns ValueTask or ValueTask<…> instead of Task or Task<…>. See the overload resolution note under Remarks.
public Optional<TResult> Select<TResult>(Func<T, TResult> selector)
where TResult : notnullProjects the underlying value through selector, returning a new optional value. If the current instance is absent, the result is Optional<TResult>.None and selector is not invoked.
| Parameter | Type | Description |
|---|---|---|
selector |
Func<T, TResult> |
The function to apply to the underlying value. |
| Type Parameter | Description |
|---|---|
TResult |
The type of the result produced by the selector. Constrained to notnull. |
Returns — An Optional<TResult> containing the result of selector if the current value is present; otherwise Optional<TResult>.None.
Asynchronously projects the underlying value into a new Optional<TResult> when present; otherwise returns Optional<TResult>.None. Overloads accept Task-returning or ValueTask-returning selectors, with and without a CancellationToken parameter.
| Overload | Returns | Delegates |
|---|---|---|
SelectAsync<TResult>(Func<T, Task<TResult>>, CancellationToken) |
Task<Optional<TResult>> |
selector: Task-returning |
SelectAsync<TResult>(Func<T, ValueTask<TResult>>, CancellationToken) |
ValueTask<Optional<TResult>> |
selector: ValueTask-returning |
SelectAsync<TResult>(Func<T, CancellationToken, Task<TResult>>, CancellationToken) |
Task<Optional<TResult>> |
selector: Task-returning (cancellable) |
SelectAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>>, CancellationToken) |
ValueTask<Optional<TResult>> |
selector: ValueTask-returning (cancellable) |
public async Task<Optional<TResult>> SelectAsync<TResult>(Func<T, Task<TResult>> selector, CancellationToken cancellationToken = default) where TResult : notnull
public async ValueTask<Optional<TResult>> SelectAsync<TResult>(Func<T, ValueTask<TResult>> selector, CancellationToken cancellationToken = default) where TResult : notnull
public async Task<Optional<TResult>> SelectAsync<TResult>(Func<T, CancellationToken, Task<TResult>> selector, CancellationToken cancellationToken = default) where TResult : notnull
public async ValueTask<Optional<TResult>> SelectAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>> selector, CancellationToken cancellationToken = default) where TResult : notnullParameters
| Parameter | Description |
|---|---|
selector |
The delegate to apply to the value of the current Optional<T> instance. |
cancellationToken |
The token to monitor for cancellation requests. |
Type Parameters
| Type Parameter | Description |
|---|---|
TResult |
The underlying type of the result produced by the selector delegate. |
Returns
-
Task<Optional<TResult>>/ValueTask<Optional<TResult>>— A newOptional<TResult>instance containing the result of the delegate if the currentOptional<T>instance has an underlying value; otherwise,Optional<TResult>.None.
Every overload that takes a Task-returning delegate has a twin that takes the equivalent ValueTask-returning delegate and returns ValueTask or ValueTask<…> instead of Task or Task<…>. See the overload resolution note under Remarks.
public Optional<TResult> SelectMany<TResult>(Func<T, Optional<TResult>> selector)
where TResult : notnullProjects the underlying value through selector and flattens the result, returning a new optional value. If the current instance is absent, the result is Optional<TResult>.None and selector is not invoked.
| Parameter | Type | Description |
|---|---|---|
selector |
Func<T, Optional<TResult>> |
The function to apply to the underlying value. |
| Type Parameter | Description |
|---|---|
TResult |
The type of the result produced by the selector. Constrained to notnull. |
Returns — The Optional<TResult> produced by selector if the current value is present; otherwise Optional<TResult>.None.
Asynchronously projects the underlying value into a new Optional<TResult> using an optional-producing selector when present, flattening the outcome; otherwise returns Optional<TResult>.None. Overloads accept Task-returning or ValueTask-returning selectors, with and without a CancellationToken parameter.
| Overload | Returns | Delegates |
|---|---|---|
SelectManyAsync<TResult>(Func<T, Task<Optional<TResult>>>, CancellationToken) |
Task<Optional<TResult>> |
selector: Task-returning |
SelectManyAsync<TResult>(Func<T, ValueTask<Optional<TResult>>>, CancellationToken) |
ValueTask<Optional<TResult>> |
selector: ValueTask-returning |
SelectManyAsync<TResult>(Func<T, CancellationToken, Task<Optional<TResult>>>, CancellationToken) |
Task<Optional<TResult>> |
selector: Task-returning (cancellable) |
SelectManyAsync<TResult>(Func<T, CancellationToken, ValueTask<Optional<TResult>>>, CancellationToken) |
ValueTask<Optional<TResult>> |
selector: ValueTask-returning (cancellable) |
public async Task<Optional<TResult>> SelectManyAsync<TResult>(Func<T, Task<Optional<TResult>>> selector, CancellationToken cancellationToken = default) where TResult : notnull
public async ValueTask<Optional<TResult>> SelectManyAsync<TResult>(Func<T, ValueTask<Optional<TResult>>> selector, CancellationToken cancellationToken = default) where TResult : notnull
public async Task<Optional<TResult>> SelectManyAsync<TResult>(Func<T, CancellationToken, Task<Optional<TResult>>> selector, CancellationToken cancellationToken = default) where TResult : notnull
public async ValueTask<Optional<TResult>> SelectManyAsync<TResult>(Func<T, CancellationToken, ValueTask<Optional<TResult>>> selector, CancellationToken cancellationToken = default) where TResult : notnullParameters
| Parameter | Description |
|---|---|
selector |
The delegate to apply to the value of the current Optional<T> instance. |
cancellationToken |
The token to monitor for cancellation requests. |
Type Parameters
| Type Parameter | Description |
|---|---|
TResult |
The underlying type of the result produced by the selector delegate. |
Returns
-
Task<Optional<TResult>>/ValueTask<Optional<TResult>>— A newOptional<TResult>instance containing the result of the delegate if the currentOptional<T>instance has an underlying value; otherwise,Optional<TResult>.None.
Every overload that takes a Task-returning delegate has a twin that takes the equivalent ValueTask-returning delegate and returns ValueTask or ValueTask<…> instead of Task or Task<…>. See the overload resolution note under Remarks.
public Result<Optional<T>> ToResult()Wraps the current instance into a new, successful Result<T>.
Returns — A successful Result<Optional<T>> containing the current Optional<T> instance.
public sealed override string ToString()Returns the string representation of the current instance. For a Some<T>, returns the underlying value's ToString() (or string.Empty if that returns null); for a None<T>, returns "None".
Returns — The string representation of the underlying value, or "None".
| Operator | Description |
|---|---|
implicit operator Optional<T>(T?) |
Converts a value of type T into an optional value. |
explicit operator T(Optional<T>) |
Extracts the underlying value, throwing if absent. |
operator == |
Determines whether two instances are equal. |
operator != |
Determines whether two instances are not equal. |
public static implicit operator Optional<T>(T? value)Implicitly converts value into an Optional<T>. A null value becomes None; any non-null value (including value-type defaults such as 0) becomes Some.
Returns — Some wrapping value if it is non-null; otherwise None.
public static explicit operator T(Optional<T> value)Explicitly extracts the underlying value of value by calling GetValueOrThrow().
Returns — The underlying value of value.
Exceptions
| Exception | Condition |
|---|---|
InvalidOperationException |
The underlying value of value is absent. |
public static bool operator ==(Optional<T>? left, Optional<T>? right)Performs an equality comparison between two instances. Returns true if left is equal to right; otherwise, false. Two null references are considered equal.
public static bool operator !=(Optional<T>? left, Optional<T>? right)Performs an inequality comparison between two instances. Returns true if left is not equal to right; otherwise, false.
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions