Lists and collection expressions
A List<T> stores an ordered sequence whose item count can grow or shrink. The type argument T specifies the element type. Unlike an array, a list exposes Count for the current number of items and Capacity for the storage available before resizing.
using System;using System.Collections.Generic;var shelf = new List<int>(4);Console.WriteLine(shelf.Count);Console.WriteLine(shelf.Capacity);shelf.Add(12);Console.WriteLine(shelf.Count);Console.WriteLine(shelf[0]);Capacity reserves space, whereas Add creates an accessible item.
Expected output (reasoned, not executed):
04112Build a list incrementally
new List<int>() creates an empty list. Add appends one element, Insert(index, item) inserts at a position, and Remove(item) removes the first matching item if present and returns whether it succeeded. RemoveAt(index) removes by position. Read and replace existing items using zero-based indexing. Include using System.Collections.Generic; to use the short type name.
Distinguish Count from Capacity
Count is the number of accessible elements. Capacity may exceed Count because the list reserves storage. A constructor argument such as new List<int>(8) reserves room but does not insert eight zeroes. Index bounds depend on Count, never Capacity. Adding can increase capacity; do not assume a particular growth multiplier.
Use collection expressions with an explicit target
C# 12 introduced collection expressions such as List<int> values = [1, 2, 3]; and int[] values = [1, 2, 3];. The target type determines what is constructed; var values = [1, 2, 3]; has no target type and is invalid. The spread element ..other includes the elements of another sequence. Do not modify a list structurally during foreach; use a separate result list or a deliberately indexed algorithm.
using System;using System.Collections.Generic;List<int> counts = [2, 5, 2];Console.WriteLine(counts.Remove(2));Console.WriteLine(counts.Remove(99));Console.WriteLine(string.Join(", ", counts));Remove deletes only the first matching value; a missing value leaves the sequence unchanged.
Expected output (reasoned, not executed):
TrueFalse5, 2using System;using System.Collections.Generic;List<int> counts = [0, 4, 0, 7];List<int> positive = [];foreach (int count in counts){ if (count > 0) positive.Add(count);}int[] packed = [..positive, 9];Console.WriteLine(string.Join(", ", packed));Console.WriteLine(counts.Count);Only the separate result list changes during enumeration. The spread expression constructs the final array.
Expected output (reasoned, not executed):
4, 7, 94Common mistake and correction
new List<int>(10)[0] fails because capacity does not create items. Add an item before indexing or use a collection expression containing initial values. Do not use Remove inside foreach on the same List<T>; it invalidates that enumerator.
Try it yourself
Create a list from [3, 0, 3, 8]. Build a separate list containing only values greater than zero, then remove the first 3 from that separate list. Print both lists and counts. Repeat with an empty input.
Next: Ranges and jagged arrays. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Lists and collection expressions — knowledge check
Apply the lesson concepts to the following scenarios.
What is Count just after new List<int>(8)?
Which property determines the index bounds?
What does Remove(2) do to [2, 5, 2]?
Which declaration supplies a target for a collection expression?
How can you filter without invalidating the source enumerator?