Option<T> Struct
Definition
Represents an optional value: either Some(value) carrying a non-null value of type
T, or None carrying nothing.
public readonly struct Option<T> : IEquatable<Option<T>>
Type Parameters
TThe type of the contained value.
- Implements
-
IEquatable<Option<T>>
- Inherited Members
- Extension Methods
Remarks
Option<T> makes the possible absence of a value explicit in a signature, replacing
null returns and bool Try…(out …) pairs with a single composable value. Chain
transformations with Map<TResult>(Func<T, TResult>) and
Bind<TResult>(Func<T, Option<TResult>>), and collapse to a final value with
Match<TResult>(Func<T, TResult>, Func<TResult>) or the GetValueOrDefault overloads.
default(Option<T>) equals None - an option that was never assigned behaves exactly like
an explicit None, so the type is total and safe to use as a field or array element without initialization.
The type distinguishes a strict and a lenient lift: Some(T) rejects
null with ArgumentNullException (a present null is treated as a bug), while the
implicit conversion from T maps null to None (absence).
Properties
IsNone
Gets a value indicating whether this option carries no value.
public bool IsNone { get; }
Property Value
IsSome
Gets a value indicating whether this option carries a value.
public bool IsSome { get; }
Property Value
None
Gets the option that carries no value.
public static Option<T> None { get; }
Property Value
Value
Gets the contained value.
public T Value { get; }
Property Value
- T
The value carried by this option.
Remarks
Prefer TryGetValue(out T), Match<TResult>(Func<T, TResult>, Func<TResult>), or the
GetValueOrDefault overloads over direct access when absence is an expected state; Value is
intended for call sites that have already established IsSome.
Exceptions
- InvalidOperationException
The option does not contain a value.
Methods
Bind<TResult>(Func<T, Option<TResult>>)
Projects the contained value into another option using the specified option-returning selector.
public Option<TResult> Bind<TResult>(Func<T, Option<TResult>> selector)
Parameters
Returns
- Option<TResult>
The option produced by the selector when a value is present; otherwise
None.
Type Parameters
TResultThe value type of the resulting option.
Remarks
The selector is not invoked when this option is None.
Exceptions
- ArgumentNullException
selectoris null.
Equals(Option<T>)
Determines whether the specified option is equal to the current instance.
public bool Equals(Option<T> other)
Parameters
otherOption<T>The option to compare with this instance.
Returns
Equals(object?)
Determines whether the specified object is equal to the current option.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare. Only another Option<T> can be equal; all other types, including null, return false.
Returns
Filter(Func<T, bool>)
Retains the contained value only when it satisfies the specified predicate.
public Option<T> Filter(Func<T, bool> predicate)
Parameters
Returns
- Option<T>
This option when a value is present and satisfies
predicate; otherwiseNone.
Remarks
The predicate is not invoked when this option is None.
Exceptions
- ArgumentNullException
predicateis null.
GetHashCode()
Returns a hash code for the current instance consistent with Equals(Option<T>).
public override int GetHashCode()
Returns
- int
The contained value's hash code when present; otherwise
0.
GetValueOrDefault()
Returns the contained value, or the default value of T when none is present.
public T? GetValueOrDefault()
Returns
- T
The contained value, or
default(T).
GetValueOrDefault(Func<T>)
Returns the contained value, or the value produced by the specified factory when none is present.
public T GetValueOrDefault(Func<T> defaultFactory)
Parameters
defaultFactoryFunc<T>The factory invoked to produce the fallback value.
Returns
- T
The contained value, or the factory's result.
Remarks
The factory is not invoked when a value is present.
Exceptions
- ArgumentNullException
defaultFactoryis null.
GetValueOrDefault(T)
Returns the contained value, or the specified fallback when none is present.
public T GetValueOrDefault(T defaultValue)
Parameters
defaultValueTThe value returned when no value is present.
Returns
- T
The contained value, or
defaultValue.
Map<TResult>(Func<T, TResult>)
Projects the contained value into a new option using the specified selector.
public Option<TResult> Map<TResult>(Func<T, TResult> selector)
Parameters
selectorFunc<T, TResult>The projection applied to the contained value.
Returns
- Option<TResult>
Some(selector(value))when a value is present and the projection is non-null; otherwiseNone.
Type Parameters
TResultThe type produced by the selector.
Examples
var length = Option<string>.Some("railway").Map(s => s.Length); // Some(7)
var none = Option<string>.None.Map(s => s.Length); // None - selector not invoked
Remarks
The selector is not invoked when this option is None. A null projection result maps to
None (the lenient lift), so a mapping chain can never produce a present null.
Exceptions
- ArgumentNullException
selectoris null.
Match(Action<T>, Action)
Invokes the matching action for the state of the option.
public void Match(Action<T> onSome, Action onNone)
Parameters
onSomeAction<T>The action invoked with the contained value when one is present.
onNoneActionThe action invoked when no value is present.
Exceptions
- ArgumentNullException
onSomeoronNoneis null.
Match<TResult>(Func<T, TResult>, Func<TResult>)
Collapses the option to a single value by invoking the matching branch.
public TResult Match<TResult>(Func<T, TResult> onSome, Func<TResult> onNone)
Parameters
onSomeFunc<T, TResult>The projection invoked with the contained value when one is present.
onNoneFunc<TResult>The factory invoked when no value is present.
Returns
- TResult
The value produced by the invoked branch.
Type Parameters
TResultThe type produced by both branches.
Examples
var label = option.Match(v => $"got {v}", () => "nothing");
Exceptions
- ArgumentNullException
onSomeoronNoneis null.
Some(T)
Creates an option carrying the specified non-null value.
public static Option<T> Some(T value)
Parameters
valueTThe value to wrap. Must not be null.
Returns
Examples
var some = Option<string>.Some("value"); // Some(value)
var none = Option<string>.None; // None
Remarks
This is the strict lift: a null argument is rejected because a present null value defeats the
purpose of the type. To map null to None instead, use the implicit conversion
from T (the lenient lift).
Exceptions
- ArgumentNullException
valueis null.
Tap(Action<T>)
Invokes the specified action with the contained value when a value is present.
public Option<T> Tap(Action<T> action)
Parameters
actionAction<T>The side effect invoked with the contained value.
Returns
- Option<T>
This option, unchanged, so calls can be chained.
Remarks
The action is not invoked when this option is None; the option itself is always returned.
Exceptions
- ArgumentNullException
actionis null.
ToResult(ResultError)
Converts the option to a result, using the specified error when no value is present.
public Result<T> ToResult(ResultError error)
Parameters
errorResultErrorThe error carried by the failure produced when this option is
None.
Returns
- Result<T>
Success(value)when a value is present; otherwiseFailure(error).
Examples
var found = Option<string>.Some("value").ToResult("missing"); // Success(value)
var missing = Option<string>.None.ToResult("missing"); // Failure(missing)
Remarks
This is the bridge from the absence-shaped Option<T> onto the failure-shaped Result<T> railway: the caller names the error that absence represents. The inverse direction is ToOption(), which discards the error.
ToString()
Returns a string representation of the option.
public override string ToString()
Returns
- string
"Some(value)"when a value is present; otherwise"None".
TryGetValue(out T)
Attempts to retrieve the contained value.
public bool TryGetValue(out T value)
Parameters
valueTWhen this method returns, the contained value if one is present; otherwise the default value.
Returns
Operators
operator ==(Option<T>, Option<T>)
Determines whether two options are equal.
public static bool operator ==(Option<T> left, Option<T> right)
Parameters
Returns
implicit operator Option<T>(T?)
Converts a value to an option, mapping null to None.
public static implicit operator Option<T>(T? value)
Parameters
valueTThe value to lift.
Returns
Examples
string? found = lookup.TryGet(key);
Option<string> option = found; // null becomes None, a value becomes Some
Remarks
This is the lenient lift: null means absence. To treat null as a bug
instead, use the strict Some(T) factory, which throws ArgumentNullException. To
lift a Nullable<T> value type, use Option.FromNullable.
operator !=(Option<T>, Option<T>)
Determines whether two options are not equal.
public static bool operator !=(Option<T> left, Option<T> right)
Parameters
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |