C# List
List
An array has a fixed size. When you create it, you decide how many items it has, and you cannot add more items later.
A List is like an array that can grow and shrink. Think of a shopping list: you add items when you need them, and you remove items when you are done.
To use a List, add this line at the top of your file:
Example
// Include the namespace that holds List
using System.Collections.Generic;
Create a List
To create a list, write List, the type of the items inside < >, and the name of the list. new List<string>() creates a new, empty list of strings.
Add() adds an item to the end of the list:
Example
List<string> cars = new List<string>();
cars.Add("Volvo");
cars.Add("BMW");
cars.Add("Ford");
cars.Add("Mazda");
foreach (string car in cars)
{
Console.WriteLine(car);
}
The foreach loop prints all the items, just like with an array.
You can also add the items when you create the list. Write them inside curly braces { }.
To find out how many items a list has, use Count (an array uses Length, but a list uses Count):
Example
List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
Console.WriteLine(cars.Count); // 4
Access and Change Items
Like in an array, each item in a list has an index. The first item has index 0, the second has index 1, and so on.
Write the index inside square brackets [ ] to get an item:
Example
List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
Console.WriteLine(cars[0]); // Volvo
Console.WriteLine(cars[2]); // Ford
To change an item, refer to its index:
Example
List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
cars[0] = "Opel";
Console.WriteLine(cars[0]); // Opel
Insert and Remove Items
Add() always adds an item to the end. To put an item at another place, use Insert(). The first value is the index, and the second value is the item:
Example
List<string> cars = new List<string> { "Volvo", "BMW", "Ford" };
cars.Insert(1, "Mazda");
foreach (string car in cars)
{
Console.WriteLine(car);
}
Mazda is now at index 1. The items after it move one place back.
To remove an item, use Remove() with the item, or RemoveAt() with the index:
Example
List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
cars.Remove("BMW");
cars.RemoveAt(0);
foreach (string car in cars)
{
Console.WriteLine(car);
}
Remove("BMW") removes BMW. Then RemoveAt(0) removes the first item, Volvo. Only Ford and Mazda are left.
Check If an Item Exists
Contains() returns true if the list has the item, and false if it does not:
Example
List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
Console.WriteLine(cars.Contains("BMW")); // True
Console.WriteLine(cars.Contains("Tesla")); // False
Loop Through a List
You have already used foreach to loop through a list. You can also use a for loop. Use Count to know when to stop:
Example
List<string> cars = new List<string> { "Volvo", "BMW", "Ford", "Mazda" };
for (int i = 0; i < cars.Count; i++)
{
Console.WriteLine(i + ": " + cars[i]);
}
Use foreach when you only need the items. Use for when you also need the index.
Sort a List
Sort() sorts the items in the list. Strings are sorted alphabetically, and numbers from the smallest to the largest.
This list stores numbers, so its type is List<int>:
Example
List<int> scores = new List<int> { 50, 12, 87, 34 };
scores.Sort();
foreach (int score in scores)
{
Console.WriteLine(score);
}