C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Methods: The first call tests for any even int. The second tests for any int greater than 3. The third checks for any int equal to 2.
Odd, EvenTip: You can, when executing the program on your computer, change the expressions in the lambda to determine the correctness of the tests.
C# program that uses Any extension method
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] array = { 1, 2, 3 };
// See if any elements are divisible by two.
bool b1 = array.Any(item => item % 2 == 0);
// See if any elements are greater than three.
bool b2 = array.Any(item => item > 3);
// See if any elements are 2.
bool b3 = array.Any(item => item == 2);
// Write results.
Console.WriteLine(b1);
Console.WriteLine(b2);
Console.WriteLine(b3);
}
}
Output
True
False
True
Then: When it finds an element that the Predicate returns true for, the true result is propagated. It uses an early-exit.