C# List Remove Methods

by Sam Allen - Updated January 9, 2010

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 List.Remove methods in System.Collections.Generic, and also review resources.

List

Removing elements

First, 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, or based on the index. This is not equivalent to assigning an element to null.

=== 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 of the program ===

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

Methods on List that remove certain elements require linear time in C#. 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.

Removing 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 of the program ===

45

Removing 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 of the program ===

345

Using RemoveAll

Here we note that 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.

(See 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.

(Do not copy this page.)

Dot Net Perls | Search
List | Convert List to Array | List and String Conversion | List Examples | List Find Methods for Searching List | Serialize List Tutorial
C# | Obsolete Attribute | True and False | Gradient Tips | Catch Examples
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen