C# List FindIndex(Predicate)
Description
List
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
List
Syntax
public int FindIndex(
Predicate<T> match
)
Parameters
match
- The Predicatedelegate that defines the conditions of the element to search for.
Returns
List.FindIndex(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
using System;//from w ww .j a v a 2 s. com
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
class Program
{
private static List<Book> Books = new List<Book>();
public static void Main(string[] args)
{
int ndx = Books.FindIndex(FindComputer);
}
private static bool FindComputer(Book bk)
{
if (bk.Genre == "Computer")
{
return true;
}else{
return false;
}
}
}
public class Book
{
public string Genre { get; set; }
}