C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
First: The first call tests each element in the array for the string value "Codex"—this returns true.
True, FalseSecond: The second call searches for an element with the value "python"—this returns false.
Third: The third call looks for any element that starts with the letter "d", and it returns true.
StartsWith, EndsWithFourth: The final call looks for the starting letter "x" and fails, returning false.
C# program that uses Array.Exists method
using System;
class Program
{
static void Main()
{
string[] array = { "cat", "dot", "Codex" };
// Use Array.Exists in different ways.
bool a = Array.Exists(array, element => element == "Codex");
bool b = Array.Exists(array, element => element == "python");
bool c = Array.Exists(array, element => element.StartsWith("d"));
bool d = Array.Exists(array, element => element.StartsWith("x"));
// Display bools.
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
}
}
Output
True
False
True
False
Then: Once the predicate reports a match, the function exits. No further elements need be searched.
Therefore: For high-performance code, using your own inlined for-loop would be faster.
Inline Optimization