Table of Contents

Option<T> Struct

Definition

Namespace
Tyne
Assembly
Tyne.Core.dll

An option encapsulates either Some(T) or None.

[JsonConverter(typeof(OptionJsonConverterFactory))]
public readonly struct Option<T> : IEquatable<Option<T>>, IEquatable<T>

Type Parameters

T

The type of value this option encapsulates.

Implements

Remarks

The purpose of Option<T> is to provide a strong construct for handling the None case. This encourages consumers to consider how to handle a missing value, rather than assuming the happy path.

In functional terms, this is a polymorphic union which encapsulates either Some(T) or None.

See Option for how to create Option<T>s.

Some(42) is considered equal to Some(42) and 42. None<int?> is considered equal to None<int?> and null.

Constructors

Option()

Creates an empty Option<T>.

Methods

Equals(in Option<T>?)

Determines whether the specified Option<T> other is equal to the current instance of Option<T>.

Equals(object?)

Determines whether the specified obj is an Option<T>, and if so is equal to the current instance.

Equals(in Option<T>)

Determines whether the specified Option<T> other is equal to the current instance of Option<T>.

Equals(T?)

Determines whether the specified T other is equal to the current instance of Option<T>.

GetHashCode()

Returns a hash code for this instance.

ToString()

Returns a string that represents this instance.

TryUnwrap(out T?)

Tries to unwrap this option.

Operators

operator ==(in Option<T>, in Option<T>)

Determines whether left is equal to right.

operator ==(in Option<T>, T?)

Determines whether left is equal to right.

operator ==(T?, in Option<T>)

Determines whether left is equal to right.

implicit operator Option<T>(in T?)

Wraps value in an Option<T>.

operator !=(in Option<T>, in Option<T>)

Determines whether left is not equal to right.

operator !=(in Option<T>, T?)

Determines whether left is not equal to right.

operator !=(T?, in Option<T>)

Determines whether left is not equal to right.

Extension Methods

Apply<T>(Option<T>, Action<T>)
MatchAsync<T>(Option<T>, Func<T, Task>, Func<Task>)
MatchAsync<T, TResult>(Option<T>, Func<T, Task<TResult>>, Func<Task<TResult>>)
Match<T, TResult>(Option<T>, Func<T, TResult>, Func<TResult>)
OrDefault<T>(Option<T>)
Or<T>(Option<T>, Func<T>)
Or<T>(Option<T>, T)
Select<T, TResult>(Option<T>, Func<T, Task<TResult>>)
Select<T, TResult>(Option<T>, Func<T, TResult>)
ToResult<T, TE>(Option<T>, Func<TE>)
ToResult<T, TE>(Option<T>, TE)
ToTask<T>(Option<T>)
ToValueTask<T>(Option<T>)

See Also