In C#, List.FindAll(match)filters a list. You give it a test lambda, and it builds a newList<T> that holds every item for which the test returns true.
The items stay in their original order, and the original list is not changed. If no item passes, you get an empty list, never null. For just the first match use Find(). To delete the matches instead of copying them, use RemoveAll().
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.FindAll(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Pass a lambda. The result is a new list you can store, loop over, or count.
A new list of all names with exactly three letters.
numbers.FindAll(n => false)
An empty list (Count is 0). Never null.
The new list holds the same item values. For classes, it holds the same objects as the source (a shallow copy), so editing an object through one list shows in the other. The LINQ Where(...).ToList() gives the same result. A null predicate throws ArgumentNullException.
Library declarations
C#
public List<T> FindAll(Predicate<T> match);
Parameters
Parameter
Description
match
Required. A Predicate<T>: a lambda or method that takes one item and returns true for items to keep. It must not be null.
Return Value
Return Value
Description
List
A new list with all matching items, in the original order. Empty if none matched. The source list is not changed.
C# Examples
Example 1 — Keep Only the Even Numbers
Filter a list into a new list and show that the original is untouched.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
List<int> evens = numbers.FindAll(n => n % 2 == 0);
Console.WriteLine(string.Join(", ", evens));
Console.WriteLine(string.Join(", ", numbers));
Console.WriteLine(evens.Count);
}
}
Output
2, 4, 6
1, 2, 3, 4, 5, 6
3
How It Works
Rule per item.n % 2 == 0 is true when n divides by 2 with no remainder. FindAll asks this for every item.
New list built. The items that pass, 2, 4, 6, are copied into a fresh list in the same order as before.
Source is the same. The second line shows all six numbers are still in numbers.
Own Count.evens.Count is 3. The new list is independent in size from the old one.
Example 2 — When Nothing Matches
The result is an empty list, not null, so you can loop over it safely.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> { "Asha", "Ben", "Chen" };
List<string> longNames = names.FindAll(s => s.Length > 10);
Console.WriteLine(longNames == null);
Console.WriteLine(longNames.Count);
foreach (string s in longNames) {
Console.WriteLine("never printed: " + s);
}
Console.WriteLine(names.FindAll(s => s.Length == 3).Count);
}
}
Output
False
0
1
How It Works
Strict rule. No name is longer than 10 letters, so no item passes the test.
Never null. The first line is False: FindAll returns a real list, even when it has nothing in it.
Safe to loop. The foreach body does not run, because there are zero items. No null check is needed before the loop.
Chain a Count. You can call .Count straight on the result. Only "Ben" has exactly three letters, so it prints 1.
Example 3 — Filter Objects, Then Change the Result
Filter products by price. Adding to the result does not touch the source list.
C#
using System;
using System.Collections.Generic;
class Product {
public string Name;
public int Price;
}
class Program {
static void Main() {
List<Product> shop = new List<Product> {
new Product { Name = "Pen", Price = 5 },
new Product { Name = "Book", Price = 40 },
new Product { Name = "Lamp", Price = 25 }
};
List<Product> cheap = shop.FindAll(p => p.Price < 30);
cheap.Add(new Product { Name = "Gift", Price = 1 });
Console.WriteLine(cheap.Count);
Console.WriteLine(shop.Count);
Console.WriteLine(cheap[0].Name + ", " + cheap[1].Name);
}
}
Output
3
3
Pen, Lamp
How It Works
Filter by a field.p.Price < 30 keeps the pen (5) and the lamp (25), and rejects the book (40).
Separate lists.cheap is a different list object. Adding "Gift" to it makes it three items long.
Source unchanged in size.shop.Count is still 3, because the add went to cheap only.
Order is kept. The first two items in cheap are the pen and the lamp, in the order they had in shop.
Frequently Asked Questions
It filters the list. It returns a new List<T> that contains every item for which your test lambda returns true.
No. The original list stays as it is. Use RemoveAll if you want to delete the matching items.
An empty list (Count is 0). It never returns null, so it is safe to loop over the result.
Almost. FindAll runs right away and returns a List<T>. Where is lazy and needs ToList() to produce a list.
The list is new, but for classes it holds the same objects as the original list (a shallow copy), so a change to an object shows in both lists.