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.
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
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.
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
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
Addis O(1) amortized. The occasional resize copies the whole array.Insert,RemoveAtandRemoveare O(n) because items shift, andRemovealso searches first. Removing the last item is O(1).RemoveAllis the efficient way to delete many items. It makes one pass and shifts each kept item once; callingRemovein a loop is quadratic.Removeremoves only the first match. For all matches useRemoveAll(x => x == value).- Removal order. When you must remove by index inside a loop, iterate from the end so the remaining indexes stay valid.
Clearkeeps the capacity. The internal array stays allocated; callTrimExcessto release it.- Order matters for equality types. If
Tis a class withoutEqualsoverride,Remove(item)uses reference equality. - Alternatives: use a
HashSet<T>if you add and remove by value often, aQueue<T>for removal at the front, or aLinkedList<T>when you hold on to nodes.