Read the syntax
List<T> holds a growable sequence of values of type T. Add appends an item, Count reports how many items are present, and a zero-based indexer reads or replaces an item.
Remove searches for a matching value, while RemoveAt removes a particular index. Sort changes the list in place; a comparison function can choose a different ordering.
Syntax
List<Type> list = new List<Type>();Examples
Creating and Adding Elements
Building a List<T> and adding items to it.
List<string> fruits = new List<string>();
fruits.Add("apple");
fruits.Add("banana");
fruits.Add("cherry");
Console.WriteLine(string.Join(", ", fruits)); // apple, banana, cherry
Console.WriteLine(fruits.Count); // 3- Building a List<T> and adding items to it.
- List<T> holds a growable sequence of values of type T. Add appends an item, Count reports how many items are present, and a zero-based indexer reads or replaces an item.
- Three Add calls append fruit names in order. Join prints apple, banana, cherry and Count prints 3.
Accessing and Modifying Elements
Reading, updating, and removing elements by index or value.
List<string> names = new List<string> { "John", "Zain", "Jamal" };
Console.WriteLine(names[1]); // Zain
names[1] = "Priya"; // replace element at index 1
Console.WriteLine(string.Join(", ", names));
names.Remove("Jamal"); // remove by value
names.RemoveAt(0); // remove by index
Console.WriteLine(string.Join(", ", names)); // Priya- Reading, updating, and removing elements by index or value.
- List<T> holds a growable sequence of values of type T. Add appends an item, Count reports how many items are present, and a zero-based indexer reads or replaces an item.
- Index 1 initially holds Zain, then is replaced with Priya. Removing Jamal by value and John by index leaves only Priya.
Iterating and Searching
Looping through elements and checking membership.
List<int> numbers = new List<int> { 10, 20, 30 };
foreach (int num in numbers)
{
Console.WriteLine(num);
}
Console.WriteLine(numbers.Contains(20)); // true
Console.WriteLine(numbers.IndexOf(30)); // 2- Looping through elements and checking membership.
- List<T> holds a growable sequence of values of type T. Add appends an item, Count reports how many items are present, and a zero-based indexer reads or replaces an item.
- foreach prints 10, 20 and 30. Contains reports True for 20, and IndexOf returns 2 for 30 because indexing starts at zero.
Sorting a List
Using the built-in Sort() method, with an optional custom comparison.
List<int> numbers = new List<int> { 5, 1, 3 };
numbers.Sort();
Console.WriteLine(string.Join(", ", numbers)); // 1, 3, 5
numbers.Sort((a, b) => b.CompareTo(a)); // descending, via a lambda
Console.WriteLine(string.Join(", ", numbers)); // 5, 3, 1- Using the built-in Sort() method, with an optional custom comparison.
- List<T> holds a growable sequence of values of type T. Add appends an item, Count reports how many items are present, and a zero-based indexer reads or replaces an item.
- The first Sort orders 1, 3, 5. The comparison b.CompareTo(a) reverses the order, so the second printed list is 5, 3, 1.
Best practices
- Use List<T> as the default resizable collection - it covers the vast majority of everyday collection needs in C#
- Declare the variable using the IList<T> or IEnumerable<T> interface in method signatures so the implementation can vary without changing calling code
- Use Contains() sparingly on large lists - for frequent membership checks, a HashSet<T> is much faster
- Specify an initial capacity (new List<int>(100)) when you know roughly how many elements to expect, to reduce internal resizing
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference