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.
public T Find (Predicate<T> match);
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:
Elements Present in List: 2 4 7 2 6 2 2 Result: 2
Elements Present in List: 5 7 9 11 3 17 19 Result: 0
- C# | Check if every List element matches the predicate conditions
- C# | How to get the last occurrence of the element in the List that match the specified conditions
- C# | How to get all elements of a List that match the conditions specified by the predicate
- C# | How to check whether a List contains the elements that match the specified conditions
- C# | Remove all elements of a List that match the conditions defined by the predicate
- C# | Check if an array contain the elements that match the specified conditions
- C# | Remove elements from a HashSet with conditions defined by the predicate
- C# | Remove the first occurrence from the StringCollection
- C# | Removing first occurrence of specified value from LinkedList<T>
- C# | Index of first occurrence in StringCollection
- C# | Removing first occurrence of object from Collection<T>
- C# | Remove the first occurrence of a specific object from the ArrayList
- How to sort a list in C# | List.Sort() Method Set -1
- How to sort a list in C# | List.Sort() Method Set -2
- C# | Capacity of a List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.