Filter
Returns the option if it IsSome and the predicate function returns true when applied it.
Otherwise, returns an Option<T>.None().
| Parameter | Returns |
|---|---|
|
Func<T, bool> predicate Option<T> option |
Option<T> |
Usage
When the option value IsNone or when the option value IsSome and the predicate function returns true, this function will returns the option parameter as a result.
Otherwise, returns an Option<T>.None().
When the option value IsSome and predicate returns true
Option<int> optionValue = 4;
Option<int> result = optionValue.Filter(value => value % 2 == 0);
//result.IsSome = true
//result.Some = 4
When the option value IsSome and predicate returns false
Option<int> optionValue = 3;
Option<int> result = optionValue.Filter(value => value % 2 == 0);
//result.IsSome = false
//result.IsNone = true
When the option value IsNone
Option<int> optionValue = Option<int>.None();
Option<int> result = optionValue.Filter(value => value % 2 == 0);
//result.IsSome = false
//result.IsNone = true