Conditionally remove items from a list in C#
This post will discuss how to conditionally remove items from a list in C#.
1. Using List<T>.RemoveAll() Method
To remove elements from a list that satisfies a given predicate in C#, you can use the List<T>.RemoveAll() method. The following code example shows a typical invocation for this method. Note that this uses LINQ.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Linq; using System.Collections.Generic; public class Company { public string name; public int founded; public Company(string name, int founded) { this.name = name; this.founded = founded; } public override string ToString() { return "[" + name + ", " + founded + "]"; } } public class Example { public static void Main() { List<Company> cars = new List<Company>(); cars.Add(new Company("Ford", 1903)); cars.Add(new Company("Chevrolet", 1908)); cars.Add(new Company("Toyota", 1937)); cars.Add(new Company("Honda", 1948)); cars.RemoveAll(s => s.founded > 1920); Console.WriteLine(String.Join(", ", cars)); } } |
Output:
[Ford, 1903], [Chevrolet, 1908]
2. Using Remove() Method
In order to remove a single occurrence of an element from a list that satisfies a given predicate in C#, you can use the List<T>.Remove() method from LINQ. This method is demonstrated below using the Single() method that returns the only element of a sequence that satisfies a specified condition.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Linq; using System.Collections.Generic; public class Company { public string name; public int founded; public Company(string name, int founded) { this.name = name; this.founded = founded; } public override string ToString() { return "[" + name + ", " + founded + "]"; } } public class Example { public static void Main() { List<Company> cars = new List<Company>(); cars.Add(new Company("Ford", 1903)); cars.Add(new Company("Chevrolet", 1908)); cars.Add(new Company("Toyota", 1937)); cars.Add(new Company("Honda", 1948)); cars.Remove(cars.Single(s => s.founded == 1948)); Console.WriteLine(String.Join(", ", cars)); } } |
Output:
[Ford, 1903], [Chevrolet, 1908], [Toyota, 1937]
The List<T>.Remove() method throws System.InvalidOperationException if the sequence contains no matching element. This can be handled using the SingleOrDefault() method that returns a default value if the matching element is not found.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using System.Linq; using System.Collections.Generic; public class Company { public string name; public int founded; public Company(string name, int founded) { this.name = name; this.founded = founded; } public override string ToString() { return "[" + name + ", " + founded + "]"; } } public class Example { public static void Main() { List<Company> cars = new List<Company>(); cars.Add(new Company("Ford", 1903)); cars.Add(new Company("Chevrolet", 1908)); cars.Add(new Company("Toyota", 1937)); cars.Add(new Company("Honda", 1948)); var toRemove = cars.SingleOrDefault(s => s.founded == 1948); if (toRemove != null) { cars.Remove(toRemove); } Console.WriteLine(String.Join(", ", cars)); } } |
Output:
[Ford, 1903], [Chevrolet, 1908], [Toyota, 1937]
Note that both Single() and SingleOrDefault() method throws System.InvalidOperationException if the sequence contains more than one matching element. Finally, in order to remove all occurrences of matching elements from a list, you can invoke the List<T>.Remove() method for each matching element. Here’s an example of its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using System.Linq; using System.Collections.Generic; public class Company { public string name; public int founded; public Company(string name, int founded) { this.name = name; this.founded = founded; } public override string ToString() { return "[" + name + ", " + founded + "]"; } } public class Example { public static void Main() { List<Company> cars = new List<Company>(); cars.Add(new Company("Ford", 1903)); cars.Add(new Company("Chevrolet", 1948)); cars.Add(new Company("Toyota", 1937)); cars.Add(new Company("Honda", 1948)); var toRemove = cars.Where(item => item.founded == 1948).ToList(); foreach (var s in toRemove) { cars.Remove(s); } Console.WriteLine(String.Join(", ", cars)); } } |
Output:
[Ford, 1903], [Toyota, 1937]
That’s all about conditionally removing items from a list in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)