C-SHARP - 4.3 Lists
This video introduces Lists in C#. A list is close to an array but more flexible: its size is not fixed, so we can keep adding elements at any time. To declare a list we type List<string> followed by a name, then allocate memory with new List<string>(). We do not need to specify the size. For readability we often use var to let the compiler infer the type.
To add an item we call list.Add(value). We can also use the initializer syntax with braces right after the parentheses. To loop over the list we run a for loop using list.Count (the equivalent of Length for arrays) and access items with list[i]. We can update an element with the index assignment, and we have two removal methods: list.Remove(value) deletes the first occurrence equal to the value, while list.RemoveAt(index) deletes the element at the given index.
var liste = new List<string>();
liste.Add("Bonjour");
liste.Add("Hello");
liste[1] = "Bonjour"; // update
liste.Remove("Bonjour"); // remove first occurrence
liste.RemoveAt(0); // remove by index
for (int i = 0; i < liste.Count; i++)
{
Console.WriteLine(liste[i]);
}
Exercise — read a football roster
- Create
var listeJoueurs = new List<string>();. - Use a
while (true)loop and ask for a player name at each turn. - If the user just presses Enter (empty string),
break;out of the loop. - Otherwise call
listeJoueurs.Add(joueur);to grow the roster.
The infinite loop adapts to any team size: the user keeps adding players and presses Enter to stop. After the loop we iterate with a for to display the roster. Lists are far more flexible than arrays for collections that grow at runtime; you will use them constantly. We will keep practising them throughout the course.
Summary
This lesson introduces C# Lists, a dynamic alternative to arrays that grow automatically as you add elements without requiring upfront size declaration. You'll learn to create Lists using List<T> or var syntax, populate them with the Add() method or inline initialization, access and modify elements by index, and remove items using Remove() or RemoveAt(). Lists are essential when your program needs to handle an unknown or variable number of items.
Key points
- Lists are dynamic collections that automatically expand as you add elements, unlike fixed-size arrays that require upfront capacity allocation
- Create Lists using explicit syntax (List<T>) or var keyword, then use .Add() method or curly-brace initialization to populate elements
- Access and modify list elements by index position using square brackets (e.g., myList[1] = "newValue"), just like arrays
- Use .Count property to retrieve the number of elements and iterate through lists with for loops, matching array syntax patterns
- Remove elements by value with .Remove(targetValue) which deletes the first match, or by position with .RemoveAt(index) for precise deletion
- Lists solve the real-world problem of unknown data sizes—ideal for programs that collect user input or variable-length datasets at runtime
FAQ
What is the key difference between C# Lists and Arrays?
Arrays have a fixed size declared at creation (e.g., new string[5]), while Lists grow dynamically as you add elements with no size limit. Use Lists when the data quantity is unknown; use Arrays for fixed, known-size collections.
How do I remove an element from a List in C#?
Use .Remove(value) to delete the first occurrence of a specific value, or .RemoveAt(index) to remove the element at a particular position. RemoveAt is useful when you know the index; Remove is useful when you know the value.
Can I initialize a List with multiple elements at once?
Yes. Use inline initialization with curly braces: new List<string> { "item1", "item2", "item3" } or add elements individually later with the .Add() method.