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
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
private static bool isEven( int i)
{
return ((i % 2) == 0);
}
public static void Main(String[] args)
{
List< int > firstlist = new List< int >();
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");
foreach ( int k in firstlist)
{
Console.WriteLine(k);
}
Console.WriteLine(" ");
Console.Write("Elements that Match: \n");
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
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
private static bool isEven( int i)
{
return ((i % 2) == 0);
}
public static void Main(String[] args)
{
List< int > firstlist = new List< int >();
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");
foreach ( int k in firstlist)
{
Console.WriteLine(k);
}
Console.WriteLine(" ");
Console.Write("Result is : ");
List< int > Result = new List< int >(firstlist.FindAll(isEven));
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:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 Aug, 2022
Like Article
Save Article