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.
var numbers = new List<int> { 4, 15, 8, 23 }; int firstBig = numbers.Find(n => n > 10); // 15
Try it
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.
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
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
Findcannot distinguish "not found" from a default value for value types. UseFindIndex,ExistsorTryGetValue-style code when0orfalseis a valid element.FindAllcopies the matches into a newList<T>. LINQWhereis lazy and does not copy until you enumerate it.- The predicate should be a pure test. Changing the list from inside it throws
InvalidOperationException. BinarySearchrequires 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>orHashSet<T>once instead of scanning repeatedly. Contains(item)andIndexOf(item)search by equality instead of a predicate. For classes without anEqualsoverride, that means reference equality.- LINQ equivalents:
Findis likeFirstOrDefault,FindAlllikeWhere(...).ToList(),ExistslikeAny,TrueForAlllikeAll. - Records compare by value, so
Contains(new Product("Pen", 1.50m))finds the pen above.