ZetCode

C# List Find

last modified October 21, 2022

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

C# list is a collection of elements of the same type. The elements can be accessed by index.

C# List Find

The Find method returns the first element that matches the given predicate.

A predicate is a single argument function that returns a boolean value.

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

Find takes a Predicate delegate as a parameter.

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

var vals = new List<int> { -2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1 };

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

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

In the example, we first word that starts with 'w' and the first element that is greater than zero.

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

The predicate is a lambda expression.

$ dotnet run
war
3

C# List FindLast

The FindLast method returns the last element that matches the given predicate.

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

This is the function's syntax.

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

var vals = new List<int> { -2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1 };

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

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

In the program, we find the last word that starts with 'w' and the last integer that is greater than zero.

$ dotnet run
water
1

C# List FindAll

The FindAll method retrieves all the elements that match the conditions defined by the specified predicate.

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

var vals = new List<int> { -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));

The program finds all words that start with 'w' and all integers that are greater than zero.

$ dotnet run
war,wrong,water
3,1,2,1,4,2,1

C# List FindIndex

The FindIndex method returns the index of the first element that matches the given predicate. It returns -1 if there was not match 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 overloaded methods take the start index and the count (number of elements to search) as parameters.

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

int n = words.FindIndex(e => e.StartsWith("w"));
Console.WriteLine($"index of the 1. word that starts with 'w' is {n}");

int n2 = words.FindIndex(5, e => e.StartsWith("w"));
Console.WriteLine($"index of the 1. word that starts with 'w' after index 5 is {n2}");

We have a list of words. We find the index of the first word that starts with 'w' from the beginning and then after index 5.

$ dotnet run
index of the 1. word that starts with 'w' is 3
index of the 1. word that starts with 'w' after index 5 is 7

C# List FindLastIndex

The FindLastIndex method returns the index of the last element that matches the given predicate. It returns -1 if there was not match 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);

This is the syntax of the methods.

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

int n = words.FindLastIndex(e => e.StartsWith("w"));
Console.WriteLine($"index of the last word that starts with 'w' is {n}");

int n2 = words.FindLastIndex(5, e => e.StartsWith("w"));
Console.WriteLine($"index of the last word that starts with 'w' after index 5 is {n2}");

In the program we find the index of the last word that starts with 'w' from the end and then after index 5.

$ dotnet run
index of the last word that starts with 'w' is 7
index of the last word that starts with 'w' after index 5 is 4

In this article, we have showed how to find list elements in C#.

List all C# tutorials.