C# & .NET Framework Loops — Repeat & Conquer
💡
Exercise 18

Foreach — The Building Inspector's Walkthrough 15 XP Medium

Ctrl+Enter Run Ctrl+S Save

When the city's building inspector arrives, they don't care about index numbers — they walk through every room from start to finish. That's exactly what the foreach loop does: it iterates over each element in a collection without needing an index variable.

string[] rooms = { "Lobby", "Office", "Lab", "Rooftop" }; foreach (string room in rooms) { Console.WriteLine($"Inspecting: {room}"); }

No i, no i++, no .Length — just pure, clean iteration. The inspector walks in, checks each room, and moves on. 🏢

How foreach works under the hood:

  • It calls GetEnumerator() on the collection
  • Each iteration calls MoveNext() and reads Current
  • When there are no more elements, the loop ends automatically
  • Works with any type that implements IEnumerable — arrays, lists, strings, and more

Strings are collections too! A string is secretly an array of char values. You can foreach over it:

string buildingName = "CityTower"; int charCount = 0; foreach (char c in buildingName) { charCount++; } Console.WriteLine($"Building name has {charCount} characters"); // Output: Building name has 9 characters

foreach vs for — when to use which?

  • Use foreach when you need to read every element — clean and safe
  • Use for when you need the index, or want to modify elements
  • foreach is read-only — you can't assign to the iteration variable
  • Trying room = "Basement" inside a foreach will cause a compiler error

⚠️ Important rule: Never modify the collection you're iterating over with foreach! Adding or removing items mid-iteration throws an InvalidOperationException. The inspector reviews the building as-is — they don't knock down walls during the inspection! 🚫🔨

You can also combine foreach with a counter to track how many items you've processed:

string[] rooms = { "Lobby", "Office", "Lab", "Rooftop" }; int count = 0; foreach (string room in rooms) { Console.WriteLine($"Inspecting: {room}"); count++; } Console.WriteLine($"{count} rooms inspected!");
📋 Instructions
**Mission: Conduct a full building inspection!** The city inspector needs your help. Write a program that: 1. Creates a `string[]` array called `rooms` with these 4 rooms: `"Lobby"`, `"Office"`, `"Lab"`, `"Rooftop"` 2. Uses a `foreach` loop to print `Inspecting: X` for each room 3. Keeps a counter and prints `4 rooms inspected!` after the loop 4. Creates a string `buildingName = "CityTower"` (9 characters) 5. Uses a `foreach` loop over the string's characters to count them 6. Prints `Building name has 9 characters` The inspector is thorough — every room and every character must be checked!
For Part 1: declare `string[] rooms = { "Lobby", "Office", "Lab", "Rooftop" };` and `int count = 0;`. Use `foreach (string room in rooms)` and increment count inside. For Part 2: declare `string buildingName = "CityTower";` and `int charCount = 0;`. Use `foreach (char c in buildingName)` to count characters.
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.