Remove
, List
For a C# List
, the Remove()
method eliminates elements. We remove by index, value, or by condition (a lambda). Each approach has its proper use.
The Remove
method is effective for removing by value. But we may also use RemoveAt
to remove an element at an index.
Remove
exampleWe can remove based on the element value (with Remove
), or based on the index (with RemoveAt
). This is not equivalent to assigning an element to null
.
List
and add several strings to it. This List
is used to demonstrate how Remove
methods work.Remove
to remove an element by its value. So the element with the value "BB" is removed.RemoveAt
. It removes the element at index 1, which is the second element, "BB."using System; using System.Collections.Generic; // Step 1: create List. List<string> values = new List<string>(); values.Add("AA"); values.Add("BB"); values.Add("CC"); // Step 2: remove by value. values.Remove("BB"); Console.WriteLine(string.Join(";", values)); // Step 3: remove by index. values.RemoveAt(1); Console.WriteLine(string.Join(";", values));AA;CC AA
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.
Math.Max
to avoid negative parameters.using System; using System.Collections.Generic; 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); }45
RemoveRange
, example 2Here 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.
using System; using System.Collections.Generic; 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); }345
RemoveAt
The RemoveAt()
method removes one element. How is this method on the List
type implemented in .NET? It requires an index argument.
List
be instantiated with three strings (the words in the name of this website).string
at index 1, which is the second string
. The List
now contains only two strings: "dot" and "perls."using System; using System.Collections.Generic; 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); }dot perls
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.
RemoveAt
is faster than Remove
. It doesn't need to iterate through the number of elements equal to index.RemoveAll
to remove all elements in the List
that match a certain predicate expression.RemoveAll
method, which can reduce line count.When you call RemoveAt
, all the element following the index you remove will be copied and shifted forward. This is not efficient.
null
or a default()
expression.Insert
method on List
suffers from a similar performance drawback. It causes excessive element copying.We invoked the Remove
, RemoveAt
, RemoveAll
and RemoveRange
methods. We found out how to remove the first and last elements, and reviewed algorithmic complexity.