Exists
Returns true if the given predicate functions return true when applied to either value. Otherwise, returns false.
ParĂ¢metros
Retorno
Func<TRight, bool> predicateWhenRight
Func<TLeft, bool> predicateWhenLeft
Either<TLeft, TRight> either
bool
Usage
When the Either
IsLeft
, the result will be the return of predicateWhenLeft
function, otherwise will be the return of predicateWhenRight
.
When Either IsRight and predicate returns true
Either<string, int> either = 20;
bool result =
either.Exists(
right => right == 20,
left => left == "Hello World");
//result = true
When Either IsLeft and predicate returns true
Either<string, int> either = "Hello World";
bool result =
either.Exists(
right => right == 20,
left => left == "Hello World");
//result = true
When Either IsRight and predicate returns false
Either<string, int> either = 15;
bool result = either.Exists(
right => right == 20,
left => left == "Hello World");
//result = false
One sided approach
You can also use the ExistsLeft
and ExistsRight
to produce the same results, but with these methods the predicated is applied just to one of the possible values.
When the target type is different from Either
current value the result always will be false
ExistsRight when Either IsRight
Either<string, int> either = 20;
bool result = either.ExistsRight(right => right == 20);
//result = true
ExistsLeft when Either IsRight
Either<string, int> either = 20;
bool result = either.ExistsLeft(left => left == "Hello World");
//result = false
Last updated
Was this helpful?