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.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary