List<T>.RemoveAll(Predicate<T>) 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 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 int RemoveAll (Predicate<T> match);
Parameter:
match: It is the Predicate<T> delegate that defines the conditions of the elements which is to be removed.
Return Value: This method returns the number of elements that to be remove from the List<T>.
Exception: This method will give ArgumentNullException if the match is null.
Below programs illustrate the use of List<T>.RemoveAll(Predicate<T>) Method:
Example 1:
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 >();
for ( int i = 1; i <= 10; i++) {
firstlist.Add(i);
}
Console.WriteLine( "Elements Present in List:\n" );
foreach ( int k in firstlist)
{
Console.WriteLine(k);
}
Console.WriteLine( " " );
Console.Write( "Number of Elements Removed: " );
Console.WriteLine(firstlist.RemoveAll(isEven));
Console.WriteLine( " " );
Console.WriteLine( "Remaining Elements in List:" );
foreach ( int k in firstlist)
{
Console.WriteLine(k);
}
}
}
|
Output:
Elements Present in List:
1
2
3
4
5
6
7
8
9
10
Number of Elements Removed: 5
Remaining Elements in List:
1
3
5
7
9
Example 2:
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(13);
firstlist.Add(17);
firstlist.Add(19);
firstlist.Add(11);
Console.WriteLine( "Elements Present in List:\n" );
foreach ( int k in firstlist)
{
Console.WriteLine(k);
}
Console.WriteLine( " " );
Console.Write( "Number of Elements Removed: " );
Console.WriteLine(firstlist.RemoveAll(isEven));
Console.WriteLine( " " );
Console.WriteLine( "Remaining Elements in List:" );
foreach ( int k in firstlist)
{
Console.WriteLine(k);
}
}
}
|
Output:
Elements Present in List:
13
17
19
11
Number of Elements Removed: 0
Remaining Elements in List:
13
17
19
11
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 :
01 Feb, 2019
Like Article
Save Article