Lesson 19: Lists & foreach
A variable holds one value; a list holds many. In C#: List<string> names = new List<string> { "Ann", "Bob" }; creates a list. names.Add("Cara"); adds an item, and names.Count gives how many. To visit every item you use foreach (string name in names) — no manual counter needed.
A list is a box with numbered slots. .Add adds a slot, .Count counts them, and foreach walks slot by slot without you counting yourself.
- List<T>
- A collection holding many values of the same type T (e.g. List<int>).
- .Add()
- Adds an item to the end of the list.
- .Count
- How many items the list currently holds.
- foreach
- A loop that visits every item in a collection: foreach (string x in list).