0
0
CsharpHow-ToBeginner · 3 min read

How to Use List.Find in C# to Search Lists Easily

Use List.Find in C# to search a list and return the first element that matches a condition specified by a Predicate. You provide a function that checks each item, and Find returns the first item where the function returns true or the default value (null for reference types) if none match.
📐

Syntax

The List.Find method searches for an element that matches the conditions defined by the specified Predicate<T> delegate and returns the first occurrence.

  • list.Find(Predicate<T> match): Calls the method on a list, passing a function that tests each item.
  • Predicate<T>: A function that takes an item and returns true if it matches the condition, otherwise false.
  • The method returns the first matching item or the default value (null for reference types) if no match is found.
csharp
T Find(Predicate<T> match);
💻

Example

This example shows how to use List.Find to find the first even number in a list of integers.

csharp
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 3, 7, 10, 15, 20 };

        // Find the first even number
        int firstEven = numbers.Find(n => n % 2 == 0);

        Console.WriteLine($"First even number: {firstEven}");
    }
}
Output
First even number: 10
⚠️

Common Pitfalls

Common mistakes when using List.Find include:

  • Not providing a valid predicate function, which can cause runtime errors.
  • Expecting Find to return all matches instead of just the first one.
  • Not handling the case when no item matches, which returns the default value (null for reference types or zero for value types).

Always check the result before using it to avoid null reference exceptions.

csharp
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> fruits = new List<string> { "apple", "banana", "cherry" };

        // Wrong: expecting Find to return all matches
        // string result = fruits.Find(f => f.Contains("a")); // returns only first match

        // Right: use Find to get first match
        string firstWithA = fruits.Find(f => f.Contains("a"));

        if (firstWithA != null)
        {
            Console.WriteLine($"First fruit containing 'a': {firstWithA}");
        }
        else
        {
            Console.WriteLine("No fruit contains 'a'.");
        }
    }
}
Output
First fruit containing 'a': apple
📊

Quick Reference

Summary tips for using List.Find:

  • Use a Predicate<T> to specify the search condition.
  • Find returns only the first matching element.
  • Check for null or default values to handle no matches.
  • For multiple matches, use FindAll instead.

Key Takeaways

List.Find returns the first item matching a condition defined by a predicate function.
Always provide a valid predicate that returns true for the desired item.
Check the result for null or default to avoid errors when no match is found.
List.Find returns only one item; use FindAll for multiple matches.
Use lambda expressions for concise and readable predicates.