C# List Remove Methods

Dot Net Perls

You are using the List collection in the C# programming language and need to remove certain elements. These can be at certain indexes, can have certain values, or match conditions. Here we see several examples of Remove and also review resources.

Illustration

Remove elements

To start, here we see how you can Remove (erase) elements from your List. You can do this 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.

List RemoveAt Explanation
Program that uses Remove method [C#] 

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

Description. This example shows Remove and RemoveAt. It removes the element with the value "bulldog", which erases the fourth element from the dogs List. It then removes the element with index 1, which is the second dog, "otterhound".

Notes

Note (please read)

Methods on List that remove certain elements require linear time. Therefore, it could be more performant to assign null to elements you want to erase them, rather than removing them. This could complicate your code, however.

List.Remove "This method performs a linear search; therefore, this method is an O(n) operation, where n is Count." [MSDN]

List.RemoveAt "This method is an O(n) operation, where n is (Count - index)." [MSDN] This means that RemoveAt will perform better than Remove, because it doesn't need to iterate through the number of elements equal to index. RemoveAt is faster.

Remove all except last elements

Here we see the RemoveRange method, which can remove elements in a certain series of indexes. One of the most useful ways to use call this method is to remove the first N or last N elements at once. Here we remove all elements except the last two. The code is robust because it uses Math.Max to avoid negative parameters.

Program that uses RemoveRange method [C#]

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

Remove first elements

Another useful way to call RemoveRange is to remove the first N elements in your List. We also use Math.Min here to avoid arguments that are too large and would raise an exception.

Program that uses RemoveRange on first elements [C#]

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

RemoveAll

Programming tip

You can use the RemoveAll method on the List type to remove all elements in the List that match a certain predicate expression. It is often easiest to use the lambda expression syntax with the RemoveAll method, which can reduce a method that contains tens of lines to a single line. This site has more information on the RemoveAll method on List.

RemoveAll List Method

Summary

In this tutorial, we saw several useful examples of using the List constructed type's Remove, RemoveAt, RemoveAll and RemoveRange methods. We found out how to remove the first N and last N elements, and also reviewed the algorithmic complexity of the methods.

Collections