C# List Find

Description

List Find searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List .

Syntax


public T Find(
  Predicate<T> match
)

Parameters

  • match - The Predicate delegate that defines the conditions of the element to search for.

Returns

List.Find method returns The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

Example

The following code uses external method to pass into Find method.


using System;/*w w w  .j  ava2 s .  c om*/
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 »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack