Option Class
Definition
Provides factory methods for creating Option<T> values with type inference at the call site.
public static class Option
- Inheritance
-
Option
- Inherited Members
Remarks
The members mirror the static surface of Option<T> so callers can write Option.Some(3) instead
of Option<int>.Some(3), and add FromNullable<T>(T?) - the Nullable<T>
lift that the implicit conversion on the generic type cannot express.
Methods
FromNullable<T>(T?)
Converts a nullable value type to an option, mapping null to None.
public static Option<T> FromNullable<T>(T? value) where T : struct
Parameters
valueT?The nullable value to lift.
Returns
- Option<T>
Some(value.Value)whenvaluehas a value; otherwiseNone.
Type Parameters
TThe underlying value type.
Examples
int? parsed = TryParseOrNull(text);
Option<int> option = Option.FromNullable(parsed); // None when parsed is null
None<T>()
Returns the option of the specified type that carries no value.
public static Option<T> None<T>()
Returns
Type Parameters
TThe value type of the option.
Some<T>(T)
Creates an option carrying the specified non-null value.
public static Option<T> Some<T>(T value)
Parameters
valueTThe value to wrap. Must not be null.
Returns
Type Parameters
TThe type of the value.
Exceptions
- ArgumentNullException
valueis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |