Beyond null – using Option
Nullable<T>
, which is embedded in C#, is a great tool to work with null
values, but there is a better and more straightforward construct for the handling of null values. Option
is a type that provides more expressive tools for conveying the presence or absence of a value, serving as a richer alternative to nullable types.
A brief introduction to Option
At its core, the Option
type can be thought of as a container that may or may not contain a value. Typically, it’s represented as either Some
(which wraps a value) or None
(indicating the absence of a value).
Usually, the implementation of Option
looks like this:
public struct Option<T> { private readonly bool _isSome; private readonly T _value; public static Option<T> None => default; public static Option<T> Some(T value) => new Option...