C# Array FindIndex(T[], Predicate)
Description
Array FindIndex
searches
for an element that matches the conditions defined by the specified predicate,
and returns the zero-based index of the first occurrence within the entire
Array.
Syntax
Array.FindIndex(T[], Predicate)
has the following syntax.
public static int FindIndex<T>(
T[] array,
Predicate<T> match
)
Parameters
Array.FindIndex(T[], Predicate)
has the following parameters.
T
- The type of the elements of the array.array
- The one-dimensional, zero-based Array to search.match
- The Predicatethat defines the conditions of the element to search for.
Returns
Array.FindIndex(T[], Predicate)
method returns The zero-based index of the first occurrence of an element that matches the
conditions defined by match, if found; otherwise, -1.
Example
//ww w . j a va 2 s .c om
using System;
public class MainClass
{
public static void Main()
{
string[] myValues ={
"1234", "19", "3456",
"3456", "200","5678",
"6543", "9876"
};
Predicate<string> myPredicate = LenCheck;
Console.WriteLine(Array.FindIndex(myValues, myPredicate));
}
private static bool LenCheck(String s)
{
if (s.Length > 3)
{
return true;
}
else
{
return false;
}
}
}
The code above generates the following result.