Search for an element that matches specified predicate, returns the zero-based index within entire List in CSharp
Description
The following code shows how to search for an element that matches specified predicate, returns the zero-based index within entire List.
Example
using System;/*from w ww . j av a2 s .co m*/
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
class Program
{
public static void Main(string[] args)
{
List<Book> Books = new List<Book>();
Book b = new Book();
b.Genre = "Computer";
Books.Add(b);
int ndx = Books.FindIndex(FindComputer);
Console.WriteLine(ndx);
}
private static bool FindComputer(Book bk)
{
if (bk.Genre == "Computer")
{
return true;
}else{
return false;
}
}
}
public class Book
{
public string Genre { get; set; }
}
The code above generates the following result.