In C#, List<T> is a resizable, ordered collection. Think of a shopping list on paper: the items have a position, you can add a new line at the end (or squeeze one in the middle), and the same item can appear twice. You put items in with Add(), ask about them with Contains(), and take them out with Remove().
A List reads an item by index in O(1) time, so list[2] is fast. Searching by value with Contains or IndexOf scans the list from the start, so it takes longer when the list is big. It lives in the System.Collections.Generic namespace, so add using System.Collections.Generic; at the top of your file.
A List never reorders itself. Items stay where you put them until you call a method such as Sort, Reverse, Insert, or Remove. That is why printing a List gives the same output on every run.
Syntax
C# Syntax
Common first steps:
C#
using System.Collections.Generic;
List<string> names = new List<string>();
names.Add("Asha"); // add at the end
names.Insert(0, "Zed"); // add at index 0
string first = names[0]; // read by index: "Zed"
bool has = names.Contains("Asha"); // true
int total = names.Count; // 2
// Create a list with starting items:
List<int> nums = new List<int> { 5, 10, 15 };
Reference
List vs Array, Dictionary, and HashSet
Pick the collection that matches what you need to do most:
Type
Stores
Order and repeats
Good for
List<T>
Items by position
Keeps order, allows repeats, can grow
An ordered collection that changes size
T[] (array)
Items by position
Keeps order, allows repeats, fixed length
A fixed number of items; simple APIs
Dictionary<TKey,TValue>
Key and value pairs
Unique keys, no promised order
Finding a value from a key
HashSet<T>
Unique items
No repeats, no promised order
Fast "is it in there?" checks and removing duplicates
A first program: add three names, read one by index, check for a name, remove it, and look at Count.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string>();
names.Add("Asha");
names.Add("Ben");
names.Add("Chen");
Console.WriteLine(names[1]);
Console.WriteLine(names.Contains("Chen"));
Console.WriteLine(names.Remove("Ben"));
Console.WriteLine(names.Count);
Console.WriteLine(names[1]);
}
}
Output
Ben
True
True
2
Chen
How It Works
Type with one part.List<string> means every item in the list is a string. The list starts empty, and Add puts each name at the end.
Index starts at 0.names[1] is the second item, "Ben". Reading an index that does not exist throws ArgumentOutOfRangeException.
Yes or no questions.Contains("Chen") is True. Remove("Ben") removes the first match and also returns True because it found one.
Items slide left. After the remove, Count is 2, and "Chen" moved from index 2 down to index 1, so names[1] now prints Chen.
Example 2 — Count Versus Capacity
Count is how many items are in the list. Capacity is how many it can hold before it must grow. The program prints only answers that are always the same.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> list = new List<int>();
Console.WriteLine(list.Count);
for (int i = 1; i <= 5; i++) {
list.Add(i * 10);
}
Console.WriteLine(list.Count);
Console.WriteLine(list.Capacity >= list.Count);
list.Clear();
Console.WriteLine(list.Count);
Console.WriteLine(list.Capacity >= 5);
}
}
Output
0
5
True
0
True
How It Works
Count counts items. A new list has Count 0. After five Add calls it is 5, because each call adds exactly one item.
Capacity is hidden room. The list keeps a bigger inside array so that Add does not need to copy everything each time. Capacity is never smaller than Count, so the comparison prints True.
Exact numbers vary. The growth rule is an internal detail, so the program does not print the capacity itself. It prints only a comparison that is true on every .NET version.
Clear keeps the room.Clear sets Count back to 0 but keeps the inside array, so Capacity is still at least 5.
Example 3 — Loop, Sort, and Search
Sort a list of numbers, loop over it with foreach, and use IndexOf and Find to look for items.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> scores = new List<int> { 70, 95, 82, 60 };
scores.Sort();
foreach (int s in scores) {
Console.WriteLine(s);
}
Console.WriteLine(scores.IndexOf(82));
Console.WriteLine(scores.Find(s => s > 90));
}
}
Output
60
70
82
95
2
95
How It Works
Collection initializer.{ 70, 95, 82, 60 } fills the list when it is created. The items keep this order until you change it.
Sort changes the list.Sort() puts the same list in ascending order, so foreach prints 60, 70, 82, 95, one per line. A List has a fixed order, so no extra sorting is needed to get steady output.
IndexOf gives a position. After sorting, 82 sits at index 2, so IndexOf(82) prints 2. A missing value would give -1.
Find uses a rule.s => s > 90 is a lambda: a tiny function that answers true or false for one item. Find returns the first item that passes, which is 95.
Continue
Next: C# List Methods Index
After you know what a List is for, open the method index for a full tutorial on each List API.
List<T> is a resizable, ordered collection in System.Collections.Generic. It keeps items by position, lets you read them by index, allows duplicates, and grows as you add items.
An array has a fixed length that you choose when you create it. A List grows and shrinks with Add, Insert, Remove, and similar methods. Both use zero-based indexes. Arrays use Length; Lists use Count.
Count is the number of items in the list right now. Capacity is the size of the hidden array behind the list, which is always at least Count. When Count would pass Capacity, the list allocates a bigger array and copies the items over.
Yes. You can add the same value many times. Remove deletes only the first match, while RemoveAll deletes every item that passes a test.
Open the C# List Methods index. It lists Add, AddRange, Insert, Remove, RemoveAll, Find, FindAll, Sort, Reverse, BinarySearch, GetRange, Slice, ToArray, ConvertAll, and every other List method, each with examples and FAQs.