C# List Remove

Remove eliminates elements from List. The target elements may be found at certain indexes—they may have certain values. They may instead match a condition we specify. The Remove method is effective for this.

Programming data: rectangle

Example

To start, we show how to 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.

RemoveAtList type

Next:This example shows Remove and RemoveAt. It removes 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".

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

Discussion

Squares: abstract

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

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

Also, you can use RemoveAll 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 line count.

RemoveAll

RemoveRange

Method call

Here we see the RemoveRange method, which 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
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

Next, 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.

Math.Min
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

Summary

We saw several useful examples of using the List 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.


C#: Collections: List