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.
public int RemoveAll (Predicate<T> match);
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:
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
Elements Present in List: 13 17 19 11 Number of Elements Removed: 0 Remaining Elements in List: 13 17 19 11
- C# | How to get all elements of a List that match the conditions specified by the predicate
- C# | Remove elements from a HashSet with conditions defined by the predicate
- C# | Remove elements from a SortedSet that match the predicate
- C# | Check if every List element matches the predicate conditions
- C# | How to check whether a List contains the elements that match the specified conditions
- C# | How to get the last occurrence of the element in the List that match the specified conditions
- C# | Check if an array contain the elements that match the specified conditions
- C# | Predicate Delegate
- C# | First occurrence in the List that matches the specified conditions
- C# | Swap two Strings without using third user defined variable
- C# | Remove all elements in a collection from a HashSet
- C# | Remove all elements from the Hashtable
- C# | Remove all elements from a HashSet
- C# | Remove all elements from the SortedSet
- C# | Remove all elements from the ArrayList
- C# | Remove all elements from a SortedList
- C# | Remove all elements from OrderedDictionary
- C# | Remove all elements from the Collection<T>
- C# | How to remove the element from the specified index of the List
- C# | Removing all the elements from the List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.