C# | First occurrence in the List that matches the specified conditions - GeeksforGeeks

30 Sep, 2019
List<T>.Find(Predicate<T>) Method is used to search for an element which matches the conditions defined by the specified predicate and it returns the first occurrence of that element within the entire List<T>.
Properties of List:
It is different from the arrays. A list can be resized dynamically but arrays cannot.
List class can accept null as a valid value for reference types and it also allows duplicate elements.
If the Count becomes equals to Capacity then the capacity of the List increases automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
Syntax:
public T Find (Predicate<T> match);

Parameter:
match: It is the Predicate delegate which defines the conditions of the element which is to be searched.
Return Value: If the element found then this method will return the first element that matches the conditions defined by the specified predicate otherwise it returns the default value for type T.
Exception: This method will give ArgumentNullException if the match is null.
Below programs illustrate the use of List<T>.Find(Predicate<T>) Method:
Example 1:
// C# Program to get the first occurrence// of the element that match the specified// conditions defined by the predicateusing System;using System.Collections;using System.Collections.Generic;  class Geeks {      // function which checks whether an    // element is even or not. Or you can    // say it is the specified condition    private static bool isEven(int i)    {        return ((i % 2) == 0);    }      // Main Method    public static void Main(String[] args)    {          // Creating an List<T> of Integers        List<int> firstlist = new List<int>();          // Adding elements to List        firstlist.Add(2);        firstlist.Add(4);        firstlist.Add(7);        firstlist.Add(2);        firstlist.Add(6);        firstlist.Add(2);        firstlist.Add(2);          Console.WriteLine("Elements Present in List:\n");          // Displaying the elements of List        foreach(int k in firstlist)        {            Console.WriteLine(k);        }          Console.WriteLine(" ");          Console.Write("Result: ");          // Will give the first occurrence of the        // element of firstlist that match the        // conditions defined by predicate        Console.WriteLine(firstlist.Find(isEven));    }}
Output:
Elements Present in List:

2
4
7
2
6
2
2
 
Result: 2

Example 2:
// C# Program to get the first occurrence// of the element that match the specified// conditions defined by the predicateusing System;using System.Collections;using System.Collections.Generic;  class Geeks {      // function which checks whether an    // element is even or not. Or you can    // say it is the specified condition    private static bool isEven(int i)    {        return ((i % 2) == 0);    }      // Main Method    public static void Main(String[] args)    {          // Creating an List<T> of Integers        List<int> firstlist = new List<int>();          // Adding elements to List        firstlist.Add(5);        firstlist.Add(7);        firstlist.Add(9);        firstlist.Add(11);        firstlist.Add(3);        firstlist.Add(17);        firstlist.Add(19);          Console.WriteLine("Elements Present in List:\n");          // Displaying the elements of List        foreach(int k in firstlist)        {            Console.WriteLine(k);        }          Console.WriteLine(" ");          Console.Write("Result: ");          // Will give the first occurrence of the        // element of firstlist that match the        // conditions defined by predicate        // No match found so it will return 0        Console.WriteLine(firstlist.Find(isEven));    }}
Output:
Elements Present in List:

5
7
9
11
3
17
19
 
Result: 0

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.find?view=netframework-4.7.2
nidhi_biet
CSharp-Collections-Namespace
CSharp-Generic-List
CSharp-Generic-Namespace
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Delegates
C# | Abstract Classes
Difference between Ref and Out keywords in C#
Extension Method in C#
C# | Class and Object
C# | Constructors
C# | Replace() Method
Introduction to .NET Framework
C# | Data Types
HashSet in C# with Examples