Using a list when the count can change
A list is like an array that can grow. You do not have to know the final count when you create it.
Adding and removing
C#
System.Collections.Generic.List<string> names =
new System.Collections.Generic.List<string>();
names.Add("Ada");
names.Add("Grace");
names.Remove("Ada");
int howMany = names.Count;Why lists are usually the better default
- You can keep adding items without resizing anything by hand.
- The count property always tells you the current size.
- You can still reach an item by its position when you need to.
Array or list? If the number of values is fixed by the problem, an array is fine. If a visitor or a database can change the count, use a list.
When you need to find a value by its name rather than its position, move on to the dictionary lesson.