The Wayback Machine - https://web.archive.org/web/20240623112652/https://www.geeksforgeeks.org/c-sharp-how-to-get-all-elements-of-a-list-that-match-the-conditions-specified-by-the-predicate/
Open In App

C# | How to get all elements of a List that match the conditions specified by the predicate

Last Updated : 30 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

List<T>.FindAll(Predicate<T>) Method is used to get all the elements that match the conditions defined by the specified predicate. 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 System.Collections.Generic.List<T> FindAll (Predicate<T> match);

Parameter:

match: It is the Predicate<T> delegate that defines the conditions of the elements which is to be searched.

Return value: This method returns a List<T> containing all the elements that match the conditions defined by the specified predicate otherwise it returns an empty List<T>. Exception: This method will give ArgumentNullException if the match is null. Below programs illustrate the use of List<T>.FindAll(Predicate<T>) Method: Example 1: 

CSharp




// C# Program to get all the element that
// match the specified conditions defined
// by the predicate
using 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(3);
        firstlist.Add(2);
        firstlist.Add(4);
 
        Console.WriteLine("Elements Present in List:\n");
 
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.WriteLine(k);
        }
 
        Console.WriteLine(" ");
 
        Console.Write("Elements that Match: \n");
 
        // Will give the List of Elements that
        // match the conditions defined by predicate
        List<int> Result = new List<int>(firstlist.FindAll(isEven));
 
        foreach(int i in Result)
        {
            Console.WriteLine(i);
        }
    }
}


Output:

Elements Present in List:

2
4
7
2
3
2
4
 
Elements that Match: 
2
4
2
2
4

Example 2: 

CSharp




// C# Program to get all the element that
// match the specified conditions defined
// by the predicate
using 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(17);
        firstlist.Add(77);
        firstlist.Add(15);
        firstlist.Add(9);
        firstlist.Add(3);
        firstlist.Add(7);
        firstlist.Add(57);
 
        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 is: ");
 
        // Will give the List of Elements that
        // match the conditions defined by predicate
        // Here no even number found in the list
        // so it will return an empty list
        List<int> Result = new List<int>(firstlist.FindAll(isEven));
 
        // checking for the resultant
        // Elements in the List
        if ((Result.Count) == 0) {
            Console.WriteLine("No Match Found");
        }
    }
}


Output:

Elements Present in List:

17
77
15
9
3
7
57
 
Result is: No Match Found

Reference:



Similar Reads

C# | Remove all elements of a List that match the conditions defined by the predicate
List&lt;T&gt;.RemoveAll(Predicate&lt;T&gt;) Method is used to remove all the elements that match the conditions defined by the specified predicate. 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 ele
3 min read
C# | How to get the last occurrence of the element in the List that match the specified conditions
List&lt;T&gt;.FindLast(Predicate&lt;T&gt;) Method is used to search for an element which matches the conditions defined by the specified predicate and it returns the last occurrence of that element within the entire List&lt;T&gt;. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot.List class can
4 min read
C# | How to check whether a List contains the elements that match the specified conditions
List&lt;T&gt;.Exists(Predicate&lt;T&gt;) Method is used to check whether the List&lt;T&gt; contains elements which match the conditions defined by the specified predicate. 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 al
3 min read
C# | Check if an array contain the elements that match the specified conditions
Array.Exists(T[], Predicate&lt;T&gt;) Method is used to check whether the specified array contains elements that match the conditions defined by the specified predicate. Syntax: public static bool Exists&lt;T&gt; (T[] array, Predicate&lt;T&gt; match); Parameters: array: It is a one-dimensional, zero-based Array to search. match: It is a Predicate t
3 min read
C# | Remove elements from a SortedSet that match the predicate
SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet&lt;T>.RemoveWhere(Predicate&lt;T>) Method is used to remove all elements that match the conditions defined by the specified predicate from a SortedSet&lt;T>. Properties: In C#, SortedSet class can be used
3 min read
C# | Remove elements from a HashSet with conditions defined by the predicate
A HashSet is an unordered collection of the unique elements. It comes under System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. HashSet&lt;T>.RemoveWhere(Predicate&lt;T>) method is used t
3 min read
C# | Check if every List element matches the predicate conditions
List&lt;T&gt;.TrueForAll(Predicate&lt;T&gt;) is used to check whether every element in the List&lt;T&gt; matches the conditions defined by the specified predicate or not. Syntax: public bool TrueForAll (Predicate&lt;T&gt; match); Parameter: match: It is the Predicate&lt;T&gt; delegate which defines the conditions to check against the elements. Retu
2 min read
C# | First occurrence in the List that matches the specified conditions
List&lt;T&gt;.Find(Predicate&lt;T&gt;) 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&lt;T&gt;. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can a
3 min read
C# | Predicate Delegate
A Predicate delegate is an in-built generic type delegate. This delegate is defined under System namespace. It works with those methods which contain some set of criteria and determine whether the passed parameter fulfill the given criteria or not. This delegate takes only one input and returns the value in the form of true or false. Now, first of
2 min read
C# | Adding the elements of the specified collection to the end of the List
List&lt;T&gt;.AddRange(IEnumerable&lt;T&gt;) Method is used to add the elements of the specified collection to the end of the List&lt;T&gt;. 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.
3 min read
three90RightbarBannerImg