C# | Remove all elements of a List that match the conditions defined by the predicate Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: CSharp // C# Program to remove all elements of // a List that match the 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 for (int i = 1; i <= 10; i++) { firstlist.Add(i); } Console.WriteLine("Elements Present in List:\n"); // Displaying the elements of List foreach (int k in firstlist) { Console.WriteLine(k); } Console.WriteLine(" "); Console.Write("Number of Elements Removed: "); // Removing the elements which is even // This will return 5 as it removed 5 // even elements from the list Console.WriteLine(firstlist.RemoveAll(isEven)); Console.WriteLine(" "); Console.WriteLine("Remaining Elements in List:"); // Displaying the elements of 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: CSharp // C# Program to remove all elements of // a List that match the 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 items to List firstlist.Add(13); firstlist.Add(17); firstlist.Add(19); firstlist.Add(11); Console.WriteLine("Elements Present in List:\n"); // Displaying the elements of List foreach(int k in firstlist) { Console.WriteLine(k); } Console.WriteLine(" "); Console.Write("Number of Elements Removed: "); // Removing the elements which is even // This will return 0 as it no elements // are even in List Console.WriteLine(firstlist.RemoveAll(isEven)); Console.WriteLine(" "); Console.WriteLine("Remaining Elements in List:"); // Displaying the elements of 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: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removeall?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Remove all elements of a List that match the conditions defined by the predicate K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Generic-List CSharp-Collections-Namespace CSharp-Generic-Namespace +1 More Similar Reads C# | How to get all elements of a List that match the conditions specified by the predicate 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 refe 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 3 min read C# | First occurrence in the List that matches the specified conditions 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 resiz 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<T>.RemoveWhere(Predicate<T>) Method is used to remove all elements that match the conditions defined by the specified predicate from a SortedSet<T 3 min read C# | Check if every List element matches the predicate conditions List<T>.TrueForAll(Predicate<T>) is used to check whether every element in the List<T> matches the conditions defined by the specified predicate or not. Syntax: public bool TrueForAll (Predicate<T> match); Parameter: match: It is the Predicate<T> delegate which defines 2 min read Like