Search for an element matching the specified predicate, and returns the first occurrence within List in CSharp
Description
The following code shows how to search for an element matching the specified predicate, and returns the first occurrence within List.
Example
using System;/*from w ww.j a va 2s. co m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
List<string> myList = new List<string>();
myList.Add("A");
myList.Add("B");
myList.Add("C");
myList.Add("D");
myList.Add("java2s.com");
string vanilla = myList.Find(FindVanilla);
Console.WriteLine(vanilla);
}
public static bool FindVanilla(string icecream)
{
return icecream.Equals("B");
}
}
The code above generates the following result.