Struct Option<T>
A generic Option type that allows for an explicit difference between an intentionally set value, and a default value of None.
Inherited Members
Namespace: Terrasoft.Common
Assembly: Terrasoft.Common.dll
Syntax
public struct Option<T>
Type Parameters
| Name | Description |
|---|---|
| T | The type to create an option for. |
Constructors
Option(T)
Creates a new instance of the Option<T> struct and initializes it with value provided by
value.
Declaration
public Option(T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | Value to initialize a new instance with. |
Properties
HasValue
true if the option has a value, false otherwise.
Declaration
public bool HasValue { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
None
The Option indicating that there is no value.
Declaration
public static Option<T> None { get; }
Property Value
| Type | Description |
|---|---|
| Option<T> |
Value
Gets the value of the option.
Declaration
public T Value { get; }
Property Value
| Type | Description |
|---|---|
| T |
Methods
As<TResult>()
Returns options of TResult type when value is instance of that type.
Declaration
public Option<TResult> As<TResult>()
where TResult : class
Returns
| Type | Description |
|---|---|
| Option<TResult> |
Type Parameters
| Name | Description |
|---|---|
| TResult | Resulting type. |
ExecuteIfHasValue(Action<T>)
Executes action when value exists.
Declaration
public void ExecuteIfHasValue(Action<T> action)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Action<T> | action | Action to execute. |
Map<TResult>(Func<T, TResult>)
Returns new option as result of converter evaluation.
Declaration
public Option<TResult> Map<TResult>(Func<T, TResult> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Func<T, TResult> | converter | Function that convert value of type |
Returns
| Type | Description |
|---|---|
| Option<TResult> |
Type Parameters
| Name | Description |
|---|---|
| TResult | Resulting type. |
Some(T)
Creates a new option from a specified value.
Declaration
public static Option<T> Some(T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | The value to create an option for. |
Returns
| Type | Description |
|---|---|
| Option<T> | A new Option<T> whose value is set to |
TryGetValue(out T)
Returns true if this option contains value; otherwise, false.
Declaration
public bool TryGetValue(out T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | Current value. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
ValueOr(T)
Returns the value of the option if it has value or otherValue otherwise.
Declaration
public T ValueOr(T otherValue)
Parameters
| Type | Name | Description |
|---|---|---|
| T | otherValue | A value to return if current value is empty. |
Returns
| Type | Description |
|---|---|
| T |
ValueOr(Func<T>)
Returns the value of the option if it has value or result of otherValueFactory
otherwise.
Declaration
public T ValueOr(Func<T> otherValueFactory)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Func<T> | otherValueFactory | Factory to return a value from if current value is empty. |
Returns
| Type | Description |
|---|---|
| T |