Skip to content

OnixLabs.Core.Optional

Matthew Layton edited this page Nov 15, 2026 · 5 revisions

Optional<T> Class

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>

Definition

public closed class Optional<T> : IValueEquatable<Optional<T>>
    where T : notnull

Type Parameters

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.

Remarks

Optional<T> models the Optional / Maybe pattern as a closed (discriminated) hierarchy with exactly two cases:

  • Some<T> — a present value, holding the underlying T in its Value property.
  • 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-null value, including value-type defaults such as 0 or Guid.Empty. Passing null throws.
  • The implicit conversion from T behaves like Some for non-null values and yields None for null. Value-type defaults are preserved as Some.
  • Of(T?) performs strip-default wrapping: any null or default value collapses to None, so Of(0), Of(Guid.Empty), and Of(default) all return None.
  • Of<TStruct>(TStruct?) lifts a Nullable<TStruct> into an Optional<TStruct>, mapping null to None and a present value to Some.

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). Prefer Some or the implicit conversion when only null should 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, and SelectManyAsync have both Task-returning and ValueTask-returning delegate overloads. An async lambda (or a lambda whose body is only a throw expression) 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 example async Task<Optional<User>> (id) => ... or async ValueTask (value) => ....

The concrete cases are documented on their own pages:

Examples

Creating optional values

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)

Matching and transforming

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)

Reading the value safely

Optional<string> optional = "value";

if (optional.HasValue)
{
    string value = optional.GetValueOrThrow();
}

string orDefault = Optional<string>.None.GetValueOrDefault("fallback"); // "fallback"

Fields

Field Type Description
None None<T> The shared singleton representing an absent optional value.

None

public static readonly None<T> None

The shared None<T> singleton instance representing an absent optional value for the type T.

Properties

Property Type Description
HasValue bool Gets a value indicating whether the underlying value is present.

HasValue

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.

Methods

Static Methods

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.

Instance Methods

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".

IsNone(Optional<T>)

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.

IsSome(Optional<T>)

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.

Of(T?)

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.

Of<TStruct>(TStruct?)

public static Optional<TStruct> Of<TStruct>(TStruct? value)
    where TStruct : struct

Creates 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.

Some(T)

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.

Equals(Optional<T>?)

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.

Equals(object?)

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.

GetHashCode()

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.

GetValueOrDefault()

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).

GetValueOrDefault(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.

GetValueOrThrow()

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>).

Match(Action<T>, Action)

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.

Match<TResult>(Func<T, TResult>, Func<TResult>)

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.

MatchAsync Overloads

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 — A Task or ValueTask (matching the overload) that represents the asynchronous operation.
  • Task<TResult> / ValueTask<TResult> — The result of the some delegate if the underlying value of the current Optional<T> instance is present; otherwise, returns the result of the none delegate if the underlying value of the current Optional<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.

Select<TResult>(Func<T, TResult>)

public Optional<TResult> Select<TResult>(Func<T, TResult> selector)
    where TResult : notnull

Projects 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.

SelectAsync Overloads

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 : notnull

Parameters

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

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.

SelectMany<TResult>(Func<T, Optional<TResult>>)

public Optional<TResult> SelectMany<TResult>(Func<T, Optional<TResult>> selector)
    where TResult : notnull

Projects 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.

SelectManyAsync Overloads

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 : notnull

Parameters

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

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.

ToResult()

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.

ToString()

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".

Operators

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.

implicit operator Optional<T>(T?)

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.

explicit operator T(Optional<T>)

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.

operator ==

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.

operator !=

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.

See Also

ONIXLabs .NET Library


Clone this wiki locally