Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Using C# List FindAll to Filter Collections

Learn how to filter a List<T> with FindAll and Predicate<T> in C#, including syntax, examples, and how it compares with LINQ Where.

~4 min readC#
#C##List<T>#Predicate#Filtering
Illustration of a C# list being filtered by a predicate, showing matching items selected into a new list.

The List<T>.FindAll method is a direct way to filter a List<T> in C#. It takes a predicate and returns a new List<T> containing only the elements that satisfy the condition. This article covers its syntax, usage, and the tradeoffs to consider when choosing it over alternatives like LINQ's Where.

Using FindAll with a Predicate

The FindAll method is defined on List<T> and accepts a Predicate<T> delegate. The predicate is a delegate that takes an element and returns a bool. FindAll evaluates the predicate for each element and builds a new list from those that return true. The original list is not modified.

csharp
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; List<int> evenNumbers = numbers.FindAll(n => n % 2 == 0);

In this example, the lambda expression n => n % 2 == 0 is the predicate. The result is a new List<int> containing 2, 4, 6, 8, 10. The original numbers list remains unchanged.

Understanding the Predicate Parameter

The predicate can be a lambda, a named method, or an anonymous delegate. The type is Predicate<T>, which is a delegate that returns bool and takes one T parameter. It has the same signature as Func<T, bool>, but the two are distinct delegate types; List<T>.FindAll specifically expects Predicate<T>.

csharp
bool IsEven(int n) => n % 2 == 0; List<int> evens = numbers.FindAll(IsEven);

Here, IsEven is a method group converted to Predicate<int>. This is useful when the filtering logic is reused elsewhere.

Practical Example: Filtering a List of Objects

Consider a Product class with Name, Price, and InStock properties. To get all in-stock products under a certain price, you can use FindAll:

csharp
public class Product { public string Name { get; set; } public decimal Price { get; set; } public bool InStock { get; set; } } List<Product> products = GetProducts(); List<Product> affordableInStock = products.FindAll(p => p.InStock && p.Price < 50);

The predicate combines two conditions. This is a common pattern for filtering domain objects without writing a loop.

FindAll vs LINQ Where

LINQ's Where method is an extension method on IEnumerable<T>. It uses deferred execution and returns an IEnumerable<T>. When you call ToList(), it materializes the result into a new list. FindAll is a method on List<T> itself and returns a List<T> directly.

AspectFindAllLINQ Where + ToList
Return typeList<T>IEnumerable<T> (then List<T>)
ExecutionImmediateDeferred until enumerated
Source typeList<T> onlyAny IEnumerable<T>
Availability.NET 2.0+.NET 3.5+ (LINQ)

For a List<T>, both approaches produce the same filtered list. The choice often comes down to style and whether you need to chain other LINQ operations. If you are already using LINQ for other parts of the query, Where is natural. If you have a List<T> and want a simple one-off filter, FindAll is direct and readable.

Performance and Allocation Considerations

FindAll allocates a new List<T> and copies matching elements into it. It examines each element once, so its time complexity is O(n). That allocation is unavoidable if you need a separate List<T> result. If you do not need the original list and want to avoid allocating a new one, RemoveAll can filter in place, but it changes the original collection and may shift elements as it removes them.

One subtle point: FindAll builds the result list as it iterates, so the result list's capacity grows as matches are added. If you know the approximate number of matches, you could use a manual loop with a pre-sized list, but for typical use this overhead is negligible. The key behavior is that FindAll does not modify the original list, which is usually the desired behavior.

Edge Cases and Common Mistakes

  • Null predicate: Passing null to FindAll throws ArgumentNullException. Always ensure the predicate is not null.
  • Empty list: If the source list is empty, FindAll returns an empty list, not null.
  • Modifying the source list inside the predicate: Do not add or remove elements from the source list while FindAll is iterating. This behavior is not documented and can lead to unexpected results or exceptions. Keep the predicate focused on deciding whether the element matches.
  • Returning a new list: Remember that the result is a new list. If you need to update the original variable, assign the result back, e.g., myList = myList.FindAll(...).

When to Use FindAll vs Other Filtering Approaches

Use FindAll when you have a List<T> and want a filtered List<T> without introducing LINQ. It is also more discoverable for developers who are not familiar with extension methods. If you are working with IEnumerable<T> or need to compose with other LINQ operators, use Where. Where is deferred, so you can process elements one at a time without materializing a full list; if you call ToList(), it materializes the result just like FindAll. For very large lists where memory is a concern, consider streaming with Where rather than materializing a new list. The decision depends on the source type, the need for a materialized result, and the surrounding code style.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.