Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Collections

List<T>.Add(), Insert() and Remove()

Reference for modifying a C# List<T>: Add, AddRange, Insert, InsertRange, Remove, RemoveAt, RemoveAll, RemoveRange and Clear, with costs and common errors.

The methods that change the contents of a List<T> fall into three groups: those that add items (Add, AddRange, Insert, InsertRange), those that remove items (Remove, RemoveAt, RemoveAll, RemoveRange, Clear) and the indexer that replaces an item in place. All of them update Count, and all of them invalidate any active foreach over the list.

Appending is cheap. Anything that touches the middle or the front of the list shifts the items behind it, which takes time proportional to the number of items moved.

C#
var list = new List<string> { "a", "c" };
list.Insert(1, "b");      // a, b, c
list.Remove("a");         // b, c
list.RemoveAt(0);         // c

Try it

C# Adding and inserting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Removing items

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public void Add(T item);
public void AddRange(IEnumerable<T> collection);
public void Insert(int index, T item);
public void InsertRange(int index, IEnumerable<T> collection);
public bool Remove(T item);
public void RemoveAt(int index);
public int RemoveAll(Predicate<T> match);
public void RemoveRange(int index, int count);
public void Clear();
Name Description
item The element to add or remove. Remove compares with EqualityComparer<T>.Default.
index For Insert, 0..Count (inclusive). For RemoveAt, 0..Count-1.
collection The sequence to insert. It may be another list, an array or any IEnumerable<T>.
match Predicate that selects the items to delete.
count Number of items to remove, starting at index.

Returns: Remove returns true if an item was removed and false otherwise. RemoveAll returns the number of removed items. The others return void.

Errors: ArgumentOutOfRangeException for an invalid index or count; ArgumentNullException for a null collection or predicate; InvalidOperationException if the list is modified while a foreach over it is running.

Usage notes

  • Add is O(1) amortized. The occasional resize copies the whole array.
  • Insert, RemoveAt and Remove are O(n) because items shift, and Remove also searches first. Removing the last item is O(1).
  • RemoveAll is the efficient way to delete many items. It makes one pass and shifts each kept item once; calling Remove in a loop is quadratic.
  • Remove removes only the first match. For all matches use RemoveAll(x => x == value).
  • Removal order. When you must remove by index inside a loop, iterate from the end so the remaining indexes stay valid.
  • Clear keeps the capacity. The internal array stays allocated; call TrimExcess to release it.
  • Order matters for equality types. If T is a class without Equals override, Remove(item) uses reference equality.
  • Alternatives: use a HashSet<T> if you add and remove by value often, a Queue<T> for removal at the front, or a LinkedList<T> when you hold on to nodes.