- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove all elements of a List that match the conditions defined by the predicate in C#
To remove all elements from a list that match the conditions defined by the predicate, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { private static bool demo(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("List elements..."); foreach (int i in list){ Console.WriteLine(i); } Console.WriteLine(" "); list.RemoveAll(demo); Console.WriteLine("List after removing elements 500..."); foreach (int i in list){ Console.WriteLine(i); } } }
Output
This will produce the following output −
List elements... 100 500 300 400 500 600 500 List after removing elements 500... 100 300 400 600
Example
Let us see another example −
using System; using System.Collections.Generic; public class Demo { private static bool demo(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("List elements..."); foreach (int i in list){ Console.WriteLine(i); } Console.WriteLine(" "); list.RemoveAll(demo); Console.WriteLine("List after removing elements..."); foreach (int i in list){ Console.WriteLine(i); } } }
Output
This will produce the following output −
List elements... 100 295 355 450 550 List after removing elements... 295 355
- Related Articles
- How to get all elements of a List that match the conditions specified by the predicate in C#?
- Remove elements from a HashSet with conditions defined by the predicate in C#
- Remove elements from a SortedSet that match the predicate in C#
- How to check whether a List contains the elements that match the specified conditions in C#?
- Check if every List element matches the predicate conditions in C#
- Check if an array contains the elements that match the specified conditions in C#
- Remove a range of elements from the List in C#
- Remove all elements from the ArrayList in C#
- Remove all elements from the SortedSet in C#
- Remove all elements from the Collection in C#
- Remove all elements from the Hashtable in C#
- First occurrence in the List that matches the specified conditions in C#
- Remove all elements from a HashSet in C#
- Remove all elements from a SortedList in C#
- Removing all the elements from the List in C#

Advertisements