Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Remove all elements of a List that match the conditions defined by the predicate in C#
The RemoveAll() method in C# removes all elements from a List<T> that match the conditions defined by a predicate. A predicate is a method that takes an element as input and returns a boolean value indicating whether the element should be removed.
Syntax
Following is the syntax for the RemoveAll() method −
public int RemoveAll(Predicate<T> match)
Parameters
match − A predicate delegate that defines the conditions for removal. It takes an element of type T and returns true if the element should be removed.
Return Value
The method returns an int representing the number of elements that were removed from the list.
Using RemoveAll() with Named Method Predicate
In this example, we create a predicate method that removes all occurrences of the value 500 −
using System;
using System.Collections.Generic;
public class Demo {
private static bool IsEqual500(int i) {
return (i == 500);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(100);
list.Add(500);
list.Add(300);
list.Add(400);
list.Add(500);
list.Add(600);
list.Add(500);
Console.WriteLine("Original list elements:");
foreach (int i in list) {
Console.WriteLine(i);
}
int removedCount = list.RemoveAll(IsEqual500);
Console.WriteLine("\nRemoved " + removedCount + " elements");
Console.WriteLine("List after removing elements 500:");
foreach (int i in list) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Original list elements: 100 500 300 400 500 600 500 Removed 3 elements List after removing elements 500: 100 300 400 600
Using RemoveAll() with Mathematical Condition
This example removes all numbers that are divisible by 10 −
using System;
using System.Collections.Generic;
public class Demo {
private static bool IsDivisibleBy10(int i) {
return ((i % 10) == 0);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(100);
list.Add(295);
list.Add(355);
list.Add(450);
list.Add(550);
Console.WriteLine("Original list elements:");
foreach (int i in list) {
Console.WriteLine(i);
}
int removedCount = list.RemoveAll(IsDivisibleBy10);
Console.WriteLine("\nRemoved " + removedCount + " elements divisible by 10");
Console.WriteLine("List after removing elements:");
foreach (int i in list) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Original list elements: 100 295 355 450 550 Removed 3 elements divisible by 10 List after removing elements: 295 355
Using RemoveAll() with Lambda Expression
You can also use lambda expressions for more concise code −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> names = new List<string> {"Alice", "Bob", "Charlie", "Anna", "David"};
Console.WriteLine("Original list:");
foreach (string name in names) {
Console.WriteLine(name);
}
int removedCount = names.RemoveAll(name => name.StartsWith("A"));
Console.WriteLine("\nRemoved " + removedCount + " names starting with 'A'");
Console.WriteLine("Updated list:");
foreach (string name in names) {
Console.WriteLine(name);
}
}
}
The output of the above code is −
Original list: Alice Bob Charlie Anna David Removed 2 names starting with 'A' Updated list: Bob Charlie David
Conclusion
The RemoveAll() method provides an efficient way to remove multiple elements from a List based on a condition. It accepts a predicate delegate, removes all matching elements in a single operation, and returns the count of removed elements.
