ForAll2
Tests if all corresponding elements of the collection satisfy the given predicate pairwise.
If any application returns false then the overall result is false and no further elements are tested.
Otherwise, true is returned.
WARNING
This function causes
IEnumerable<T>evaluation.
| Parameters | Returns |
|---|---|
Func<T, T2, bool> predicate IEnumerable<T> source IEnumerable<T2> source2 |
bool |
Usage
Verifying that all values in both collections are even numbers
//IEnumerable<int> source = { 2, 4, 6, 8, 10 }
//IEnumerable<int> source2 = { 4, 2 }
bool result =
source.ForAll2(
source2,
(element1, element2) => element1 % 2 == 0
&& element2 % 2 == 0);
//result = true
//IEnumerable<int> source = { 2, 4, 6, 8, 10 }
//IEnumerable<int> source2 = { 4, 2, 10, 12, 16 }
bool result =
source.ForAll2(
source2,
(element1, element2) => element1 % 2 == 0
&& element2 % 2 == 0);
//result = true
Creating an true result even when one of the collections contains an odd number
It is necessary to be careful, different sized collections can create situations like this one:
//IEnumerable<int> source = { 2, 4, 6, 8, 10 }
//IEnumerable<int> source2 = { 4, 2, 10, 12, 16, 1 }
bool result =
source.ForAll2(
source2,
(element1, element2) => element1 % 2 == 0
&& element2 % 2 == 0);
//result = true
The result was true because of the loop runs only until the smallest collection length, therefore, the last element of source2 never was checked.