Search element for predicate, returns the first occurrence within List in CSharp
Description
The following code shows how to search element for predicate, returns the first occurrence within List.
Example
/*ww w. j a v a 2 s .c o m*/
using System;
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);
b = new Book();
b.Genre = "Computer";
Books.Add(b);
int ndx = Books.FindIndex(1,2,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; }
}