In C#, List.Find(match) returns the first item that passes a test. The test is a lambda such as n => n > 10: a small function that takes one item and returns true or false.
The scan goes from index 0 and stops at the first true. If no item passes, the method does not throw. It returns default(T), which is 0 for numbers and null for reference types. That can look like a real result, so see the second example before you rely on it.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.Find(...). 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 that says whether an item is the one you want. The item itself comes back, not its index.
C#
List<int> numbers = new List<int> { 4, 15, 22, 9, 30 };
int firstBig = numbers.Find(n => n > 10); // 15
int none = numbers.Find(n => n > 100); // 0 (default)
Call
What it does
numbers.Find(n => n > 10)
The first number above 10.
names.Find(s => s.Length > 4)
The first long name, or null if there is none.
items.Find(p => p.Id == 2)
The first object whose Id is 2. Very common with classes.
Find is O(n). To get every match use FindAll(), and for the right-most match use FindLast(). A null predicate throws ArgumentNullException. Array.Find is the static version for arrays.
Library declarations
C#
public T Find(Predicate<T> match);
Parameters
Parameter
Description
match
Required. A Predicate<T>: a lambda or method that takes one item and returns true for the item you want. It must not be null.
Return Value
Return Value
Description
T
The first item that passes the test.
default(T)
No item passed. This is 0 for int, false for bool, and null for classes and strings.
C# Examples
Example 1 — Find the First Big Number
Four of the five numbers could match. Only the first one is returned.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 4, 15, 22, 9, 30 };
Console.WriteLine(numbers.Find(n => n > 10));
Console.WriteLine(numbers.Find(n => n % 2 == 0));
Console.WriteLine(numbers.Find(n => n > 100));
}
}
Output
15
4
0
How It Works
Test, one by one. The lambda n => n > 10 runs for 4 (false), then 15 (true). The scan stops and 15 is returned.
Later matches ignored.22 and 30 also pass, but Find never gets to them.
The very first item.4 is even, so the second rule returns it straight away.
Nothing above 100. The result is 0, the default of int. It is not an error, and it is not a number from the list.
Example 2 — The Missing Result Trap
Show that 0 can mean "found a zero" or "found nothing", and how Exists removes the doubt.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> data = new List<int> { 0, 5 };
Console.WriteLine(data.Find(n => n == 0));
Console.WriteLine(data.Find(n => n == 7));
Console.WriteLine(data.Exists(n => n == 7));
List<string> names = new List<string> { "Asha", "Ben" };
string hit = names.Find(s => s.Length > 10);
Console.WriteLine(hit == null ? "(none)" : hit);
}
}
Output
0
0
False
(none)
How It Works
Real zero. The list holds a 0 and the first call finds it, so 0 is a genuine hit.
Fake zero. No item equals 7, so Find returns default(int), which is also 0. From the output alone you cannot tell the two cases apart.
Exists gives the truth. The third line is False, so the previous 0 was only the default.
Reference types use null. No name is longer than 10 letters, so hit is null. Always test it before use, or you will hit a NullReferenceException.
Example 3 — Find an Object by Property
Find a product by its id. The returned object is the one stored in the list, so changing it changes the list.
C#
using System;
using System.Collections.Generic;
class Product {
public int Id;
public string Name;
public int Price;
}
class Program {
static void Main() {
List<Product> shop = new List<Product> {
new Product { Id = 1, Name = "Pen", Price = 5 },
new Product { Id = 2, Name = "Book", Price = 40 }
};
Product found = shop.Find(p => p.Id == 2);
found.Price = 45;
Console.WriteLine(found.Name);
Console.WriteLine(shop[1].Price);
}
}
Output
Book
45
How It Works
Search by id.p => p.Id == 2 checks the Id field of each product and matches the second one.
Same object, not a copy.Product is a class, so found points to the object inside the list.
Edit is visible. Setting found.Price = 45 changed the stored product, so shop[1].Price reads 45.
Guard in real code. If the id did not exist, found would be null and found.Price would crash. Check for null first.
Frequently Asked Questions
The first item that passes your test lambda. If no item passes, it returns default(T): 0 for numbers, false for bool, and null for classes.
No. It returns the default value. Check for null (reference types) or use Exists or FindIndex when the default could be a real value.
Use FindAll, which returns a new List<T> with every item that passes the test.
Find returns the item itself. FindIndex returns its index, or -1 when nothing matches, which is a clearer "not found" signal.
They work the same way. List.Find is an instance method (list.Find(test)), and Array.Find is a static method (Array.Find(arr, test)).