ZetCode

C# List Find

last modified November 6, 2026

In this article we show how to locate elements in a C# List with the Find, FindLast, FindAll, FindIndex, and FindLastIndex methods.

A List<T> is a dynamic collection of elements of the same type, accessible by their index. All the finding methods take a Predicate<T>: a function that takes one element and returns true if the element matches the condition. We usually pass a lambda expression.

MethodReturnsWhen nothing matches
Findfirst matching elementdefault(T)
FindLastlast matching elementdefault(T)
FindAllnew list of all matching elementsempty list
FindIndexindex of the first match-1
FindLastIndexindex of the last match-1
Existstrue if any element matchesfalse
TrueForAlltrue if all elements matchtrue for an empty list

C# List Find

The Find method returns the first element that satisfies the predicate.

public T? Find(Predicate<T> match);

The list is searched from the beginning, and the search stops at the first match.

Program.cs
List<string> words = ["sky", "cup", "new", "war", "wrong", "crypto",
    "forest", "water", "cup"];

List<int> vals = [-2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1];

string? w = words.Find(e => e.StartsWith('w'));
Console.WriteLine(w);

int n = vals.Find(e => e > 0);
Console.WriteLine(n);

string? z = words.Find(e => e.StartsWith('z'));
Console.WriteLine(z ?? "no match");

We find the first word starting with 'w' and the first positive integer.

string? w = words.Find(e => e.StartsWith('w'));

The lambda expression is the predicate. It is called for the elements in turn until it returns true. The StartsWith(char) overload compares a single character and does not depend on the culture.

string? z = words.Find(e => e.StartsWith('z'));
Console.WriteLine(z ?? "no match");

For reference types, Find returns null when no element matches, so the result is declared as nullable string?.

$ dotnet run
war
3
no match

Missing value type elements

For value types such as int, Find returns default(T), which is 0, when nothing matches. A 0 can also be a real element, so the result alone cannot tell the two cases apart.

Program.cs
List<int> vals = [-2, -1, 3, 0, 1, 2];

int n = vals.Find(e => e > 10);
Console.WriteLine($"Find: {n}");

int idx = vals.FindIndex(e => e > 10);
Console.WriteLine($"FindIndex: {idx}");

bool found = vals.Exists(e => e > 10);
Console.WriteLine($"Exists: {found}");

int first = vals.FirstOrDefault(e => e > 10, int.MinValue);
Console.WriteLine($"FirstOrDefault: {first}");

int zero = vals.Find(e => e == 0);
Console.WriteLine($"Find zero: {zero}, exists: {vals.Exists(e => e == 0)}");

We search for an element greater than 10, which is not in the list, and then for 0, which is.

int n = vals.Find(e => e > 10);

Find returns 0 although there is no such element.

int idx = vals.FindIndex(e => e > 10);

FindIndex returns -1 for a missing element, which is never a valid index. It is the reliable check for value types.

bool found = vals.Exists(e => e > 10);

If we only need to know whether a match exists, Exists is the clearest choice.

int first = vals.FirstOrDefault(e => e > 10, int.MinValue);

The LINQ FirstOrDefault method has an overload that takes the value to return when nothing matches.

$ dotnet run
Find: 0
FindIndex: -1
Exists: False
FirstOrDefault: -2147483648
Find zero: 0, exists: True

C# List FindLast

The FindLast method returns the last element that matches the predicate. It searches the list backward, from the last element to the first.

public T? FindLast(Predicate<T> match);

Like Find, it returns default(T) when no element matches.

Program.cs
List<string> words = ["sky", "cup", "new", "war", "wrong", "crypto",
    "forest", "water", "cup"];

List<int> vals = [-2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1];

string? w = words.FindLast(e => e.StartsWith('w'));
Console.WriteLine(w);

int n = vals.FindLast(e => e < 0);
Console.WriteLine(n);

We find the last word starting with 'w' and the last negative integer.

$ dotnet run
water
-2

C# List FindAll

The FindAll method returns all elements that satisfy the predicate in a new List<T>. The order of the elements is preserved and the original list is not modified.

public List<T> FindAll(Predicate<T> match);

The method never returns null.

Program.cs
List<string> words = ["sky", "cup", "new", "war", "wrong", "crypto",
    "forest", "water", "cup"];

List<int> vals = [-2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1];

List<string> res = words.FindAll(e => e.StartsWith('w'));
Console.WriteLine(string.Join(", ", res));

List<int> res2 = vals.FindAll(e => e > 0);
Console.WriteLine(string.Join(", ", res2));

List<int> res3 = vals.FindAll(e => e > 100);
Console.WriteLine($"no match: {res3.Count} elements");

We collect all words starting with 'w' and all positive integers.

List<int> res3 = vals.FindAll(e => e > 100);

When nothing matches, we get an empty list.

$ dotnet run
war, wrong, water
3, 1, 2, 1, 4, 2, 1
no match: 0 elements

C# List FindIndex

The FindIndex method returns the zero-based index of the first element that matches the predicate, or -1 if no element is found.

public int FindIndex(Predicate<T> match);
public int FindIndex(int startIndex, Predicate<T> match);
public int FindIndex(int startIndex, int count, Predicate<T> match);

The overloads restrict the search to a range that begins at startIndex and contains count elements. An invalid range throws ArgumentOutOfRangeException.

Program.cs
List<string> words = ["sky", "cup", "new", "war", "wrong", "crypto",
    "forest", "water", "cup"];

int n1 = words.FindIndex(e => e.StartsWith('w'));
Console.WriteLine($"first 'w' word: {n1}");

int n2 = words.FindIndex(5, e => e.StartsWith('w'));
Console.WriteLine($"first 'w' word from index 5: {n2}");

int n3 = words.FindIndex(5, 2, e => e.StartsWith('w'));
Console.WriteLine($"first 'w' word in indexes 5-6: {n3}");

var positions = new List<int>();
int i = -1;

while ((i = words.FindIndex(i + 1, e => e.StartsWith('w'))) != -1)
{
    positions.Add(i);
}

Console.WriteLine($"all 'w' words: {string.Join(", ", positions)}");

We search the whole list, the list from index 5, and only the indexes 5 and 6.

int n3 = words.FindIndex(5, 2, e => e.StartsWith('w'));

The range contains the words "crypto" and "forest", so the result is -1.

while ((i = words.FindIndex(i + 1, e => e.StartsWith('w'))) != -1)
{
    positions.Add(i);
}

The startIndex parameter lets us find the positions of all matches. Each search starts just after the previous match.

$ dotnet run
first 'w' word: 3
first 'w' word from index 5: 7
first 'w' word in indexes 5-6: -1
all 'w' words: 3, 4, 7

C# List FindLastIndex

The FindLastIndex method returns the zero-based index of the last element that matches the predicate, or -1 if no match is found.

public int FindLastIndex(Predicate<T> match);
public int FindLastIndex(int startIndex, Predicate<T> match);
public int FindLastIndex(int startIndex, int count, Predicate<T> match);

The search goes backward. The startIndex is the last index of the range, so FindLastIndex(8, 3, ...) searches the indexes 8, 7, and 6.

Program.cs
List<string> words = ["sky", "cup", "new", "war", "wrong", "crypto",
    "forest", "water", "cup"];

int n1 = words.FindLastIndex(e => e.StartsWith('w'));
Console.WriteLine($"last 'w' word: {n1}");

int n2 = words.FindLastIndex(5, e => e.StartsWith('w'));
Console.WriteLine($"last 'w' word in indexes 0-5: {n2}");

int n3 = words.FindLastIndex(8, 3, e => e.StartsWith('c'));
Console.WriteLine($"last 'c' word in indexes 6-8: {n3}");

int n4 = words.FindLastIndex(5, 2, e => e.StartsWith('w'));
Console.WriteLine($"last 'w' word in indexes 4-5: {n4}");

We find the last matching words in several ranges.

int n2 = words.FindLastIndex(5, e => e.StartsWith('w'));

The search starts at index 5 and goes toward the beginning of the list. The word "water" at index 7 is outside the range, so we get the index of "wrong".

$ dotnet run
last 'w' word: 7
last 'w' word in indexes 0-5: 4
last 'c' word in indexes 6-8: 8
last 'w' word in indexes 4-5: 4

Finding objects

The finding methods are most useful with lists of objects, where we search by one or more properties.

Program.cs
List<User> users =
[
    new("John Doe", "gardener", 1230),
    new("Roger Roe", "driver", 1460),
    new("Lucy Novak", "teacher", 2100),
    new("Paul Brown", "driver", 1650),
    new("Anna Smith", "teacher", 1980),
];

User? u = users.Find(u => u.Name == "Lucy Novak");
Console.WriteLine(u);

User? richDriver = users.FindLast(u => u.Occupation == "driver" && u.Salary > 1500);
Console.WriteLine(richDriver?.Name ?? "none");

User? pilot = users.Find(u => u.Occupation == "pilot");
Console.WriteLine(pilot?.Name ?? "no pilot");

List<User> teachers = users.FindAll(u => u.Occupation == "teacher");
teachers.ForEach(Console.WriteLine);

record User(string Name, string Occupation, int Salary);

We have a list of users. We find users by name, by occupation and salary, and by occupation.

User? richDriver = users.FindLast(u => u.Occupation == "driver" && u.Salary > 1500);

A predicate can combine several conditions.

User? pilot = users.Find(u => u.Occupation == "pilot");
Console.WriteLine(pilot?.Name ?? "no pilot");

There is no pilot, so Find returns null. The null-conditional operator ?. and the null-coalescing operator ?? handle the missing user.

teachers.ForEach(Console.WriteLine);

The ForEach method of the list calls the given action for each element. Records have a generated ToString method that prints their properties.

$ dotnet run
User { Name = Lucy Novak, Occupation = teacher, Salary = 2100 }
Paul Brown
no pilot
User { Name = Lucy Novak, Occupation = teacher, Salary = 2100 }
User { Name = Anna Smith, Occupation = teacher, Salary = 1980 }

Exists, TrueForAll, and reusable predicates

The Exists and TrueForAll methods check a condition without returning elements. A predicate can be stored in a variable and passed to several methods.

Program.cs
List<int> vals = [4, 8, 15, 16, 23, 42];

Console.WriteLine(vals.Exists(e => e > 40));
Console.WriteLine(vals.TrueForAll(e => e > 0));
Console.WriteLine(vals.TrueForAll(e => e % 2 == 0));

Predicate<int> isOdd = e => e % 2 != 0;

Console.WriteLine(vals.Find(isOdd));
Console.WriteLine(vals.FindIndex(isOdd));
Console.WriteLine(string.Join(", ", vals.FindAll(isOdd)));

int removed = vals.RemoveAll(isOdd);
Console.WriteLine($"removed {removed}: {string.Join(", ", vals)}");

We check the conditions on a list of integers and use one predicate with four different methods.

Predicate<int> isOdd = e => e % 2 != 0;

The Predicate<int> delegate holds the condition.

int removed = vals.RemoveAll(isOdd);

The RemoveAll method removes the matching elements from the list and returns their number. Unlike the finding methods, it modifies the list.

$ dotnet run
True
True
False
15
2
15, 23
removed 2: 4, 8, 16, 42

List methods versus LINQ

LINQ has similar methods: FirstOrDefault, LastOrDefault, and Where. Arrays have the static Array.Find, Array.FindIndex, and related methods.

Program.cs
List<string> words = ["sky", "cup", "new", "war", "wrong", "crypto",
    "forest", "water", "cup"];

Console.WriteLine(words.Find(e => e.Length == 5));
Console.WriteLine(words.FirstOrDefault(e => e.Length == 5));

Console.WriteLine(words.FindLast(e => e.Length == 5));
Console.WriteLine(words.LastOrDefault(e => e.Length == 5));

Console.WriteLine(string.Join(", ", words.FindAll(e => e.Length == 5)));
Console.WriteLine(string.Join(", ", words.Where(e => e.Length == 5)));

string[] arr = [.. words];

Console.WriteLine(Array.Find(arr, e => e.Length == 6));
Console.WriteLine(Array.FindIndex(arr, e => e.Length == 6));

We compare the List methods with their LINQ counterparts.

Console.WriteLine(string.Join(", ", words.Where(e => e.Length == 5)));

FindAll builds a new list immediately, while Where returns a lazy sequence that is evaluated when it is enumerated. The List methods are available only on List<T>; the LINQ methods work with any IEnumerable<T>.

string[] arr = [.. words];

Console.WriteLine(Array.Find(arr, e => e.Length == 6));
Console.WriteLine(Array.FindIndex(arr, e => e.Length == 6));

The Array class provides static versions of the methods for arrays.

$ dotnet run
wrong
wrong
water
water
wrong, water
wrong, water
crypto
5

Source

List<T>.Find method

In this article we have searched C# lists with the Find, FindLast, FindAll, FindIndex, FindLastIndex, Exists, and TrueForAll methods.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

Explore all C# tutorials.