Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Collections

List<T>.Find() and FindAll()

Reference for searching a C# List<T>: Find, FindAll, FindIndex, FindLast, Exists, TrueForAll, Contains and BinarySearch, with default values and LINQ comparisons.

List<T> offers a family of predicate-based search methods: Find returns the first item that matches, FindAll returns every match as a new list, FindIndex returns a position, and Exists and TrueForAll answer yes/no questions. A predicate is a Predicate<T>, usually written as a lambda such as x => x > 10.

All of them scan the list from the start (or end, for the Last variants), so they take O(n) time. They exist alongside LINQ (First, Where, Any) and are slightly cheaper because they avoid allocating an enumerator.

C#
var numbers = new List<int> { 4, 15, 8, 23 };
int firstBig = numbers.Find(n => n > 10);   // 15

Try it

C# Find, FindAll and friends

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# The default-value trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public T? Find(Predicate<T> match);
public T? FindLast(Predicate<T> match);
public List<T> FindAll(Predicate<T> match);
public int FindIndex(Predicate<T> match);
public int FindIndex(int startIndex, Predicate<T> match);
public int FindIndex(int startIndex, int count, Predicate<T> match);
public int FindLastIndex(Predicate<T> match);
public bool Exists(Predicate<T> match);
public bool TrueForAll(Predicate<T> match);
public int BinarySearch(T item);
Name Description
match The condition. Called once per examined element until the search can stop.
startIndex Where the scan begins.
count How many elements to examine from startIndex.
item For BinarySearch, the value to locate in a sorted list.

Returns: Find and FindLast return the first or last match, or default(T) (null, 0, false) if there is none. FindAll always returns a list, empty if nothing matched. FindIndex returns -1 when nothing matched. Exists and TrueForAll return bool; TrueForAll is true for an empty list. BinarySearch returns the index or the bitwise complement of the insertion point.

Errors: ArgumentNullException if match is null; ArgumentOutOfRangeException for a bad startIndex or count.

Usage notes

  • Find cannot distinguish "not found" from a default value for value types. Use FindIndex, Exists or TryGetValue-style code when 0 or false is a valid element.
  • FindAll copies the matches into a new List<T>. LINQ Where is lazy and does not copy until you enumerate it.
  • The predicate should be a pure test. Changing the list from inside it throws InvalidOperationException.
  • BinarySearch requires sorting with the same comparer. It runs in O(log n); on unsorted data its result is meaningless.
  • Many lookups by the same key? Build a Dictionary<TKey,TValue> or HashSet<T> once instead of scanning repeatedly.
  • Contains(item) and IndexOf(item) search by equality instead of a predicate. For classes without an Equals override, that means reference equality.
  • LINQ equivalents: Find is like FirstOrDefault, FindAll like Where(...).ToList(), Exists like Any, TrueForAll like All.
  • Records compare by value, so Contains(new Product("Pen", 1.50m)) finds the pen above.