HomeSearch

C# List Remove Examples

Use Remove, RemoveRange and RemoveAt on Lists. Delete elements by value and index.
Remove, List. In the winter storm, a branch is removed from the tree. For a List, the Remove() method eliminates elements. We remove by index, value, or by condition (a lambda).List
Many methods. The Remove method is effective for removing by value. But we may also use RemoveAt to remove an element at an index.
Remove example. You can Remove based on the element value you want to remove (with Remove), or based on the index (with RemoveAt). This is not equivalent to assigning an element to null.

Next: This example shows Remove and RemoveAt. It deletes the element with the value "bulldog", which erases the fourth element.

Then: It removes the element with index 1, which is the second dog, "otterhound".

C# program that uses Remove on List using System; using System.Collections.Generic; class Program { static void Main() { List<string> dogs = new List<string>(); dogs.Add("maltese"); // Contains maltese dogs.Add("otterhound"); // maltese, otterhound dogs.Add("rottweiler"); // maltese, otterhound, rottweiler dogs.Add("bulldog"); // ... rottweiler, bulldog dogs.Add("whippet"); // .... rottweiler, bulldog, whippet dogs.Remove("bulldog"); // Remove bulldog foreach (string dog in dogs) { Console.WriteLine(dog); } // Contains: maltese, otterhound, rottweiler, whippet dogs.RemoveAt(1); // Remove second dog foreach (string dog in dogs) { Console.WriteLine(dog); } // Contains: maltese, rottweiler, whippet } } Output maltese otterhound rottweiler whippet maltese rottweiler whippet
Notes, performance. Methods on List that remove elements require linear time. It could be faster to assign null to elements you want to erase, rather than removing them.

Note: RemoveAt is faster than Remove. It doesn't need to iterate through the number of elements equal to index.

RemoveAll: You can use RemoveAll to remove all elements in the List that match a certain predicate expression.

Tip: It is often easiest to use the lambda expression syntax with the RemoveAll method, which can reduce line count.

RemoveAll
RemoveRange. This can remove elements in a certain series of indexes. One useful way to use this method is to remove the first or last several elements at once.

Next: We remove all elements except the last two. The code is robust because it uses Math.Max to avoid negative parameters.

Math.Max, Min
C# program that uses RemoveRange method using System; using System.Collections.Generic; class Program { static void Main() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.Add(5); // Remove all except last 2 int remove = Math.Max(0, list.Count - 2); list.RemoveRange(0, remove); foreach (int i in list) { Console.Write(i); } } } Output 45
RemoveRange, example 2. Here we use RemoveRange to remove the first N elements in a List. We also use Math.Min here to avoid arguments that are too large and would raise an exception.
C# program that uses RemoveRange on first elements using System; using System.Collections.Generic; class Program { static void Main() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.Add(5); // Remove first 2 elements int remove = Math.Min(list.Count, 2); list.RemoveRange(0, remove); foreach (int i in list) { Console.Write(i); } } } Output 345
RemoveAt. RemoveAt removes one element. How is this method on the List type implemented in the .NET Framework? It requires an index argument.

Example: In this program we specify that a List be instantiated with three strings: "dot", "net" and "perls".

Next: We remove the string at index 1, which is the second string. The List now contains only two strings: "dot" and "perls".

VarString Literal
C# program that uses RemoveAt using System; using System.Collections.Generic; class Program { static void Main() { var list = new List<string>(); list.Add("dot"); list.Add("net"); list.Add("perls"); list.RemoveAt(1); foreach (string element in list) Console.WriteLine(element); } } Output dot perls
Discussion. When you call RemoveAt, all the element following the index you remove will be copied and shifted forward. This is not efficient.

However: If you want to remove an element but eliminate copying, you could simply assign it to null or default(T).

NullDefault

Also: The Insert method on List suffers from a similar performance drawback. It causes excessive element copying.

Insert
A summary. We used List type's Remove, RemoveAt, RemoveAll and RemoveRange methods. We found out how to remove the first and last elements, and reviewed algorithmic complexity.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls