Iterating Collections
Lists, arrays, dictionaries.
Iterating Collections is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Collections You Can Iterate
Many C# types can be looped with foreach: arrays, List<T>, dictionaries, and more.
They all support iteration because they implement a shared interface. You don't need to know the details to use them.
Iterating an Array
Arrays have a fixed size set when created.
You can loop over an array of any element type the same way.
double[] prices = { 9.99, 4.50, 19.00 };
foreach (var p in prices)
{
Console.WriteLine(p);
}Iterating a List
A List<T> can grow and shrink. It is one of the most common collections.
Looping over it looks exactly like looping over an array.
var fruits = new List<string> { "apple", "pear" };
foreach (var f in fruits)
{
Console.WriteLine(f);
}Counting While Iterating
Sometimes you want a manual counter inside foreach.
Declare a variable before the loop and increment it on each pass.
var items = new List<string> { "a", "b", "c" };
int i = 0;
foreach (var item in items)
{
Console.WriteLine(i + ": " + item);
i++;
}Iterating a Dictionary
A Dictionary<K,V> stores key-value pairs.
Looping over it gives you a KeyValuePair with .Key and .Value.
var ages = new Dictionary<string, int>
{
["Ada"] = 36,
["Linus"] = 54
};
foreach (var pair in ages)
{
Console.WriteLine(pair.Key + "=" + pair.Value);
}Dictionary Keys and Values
If you only need keys or only values, use the .Keys or .Values properties.
Each is itself a collection you can loop over.
foreach (var name in ages.Keys)
{
Console.WriteLine(name);
}Iterating a HashSet
A HashSet<T> stores unique values. Duplicates are ignored.
You can foreach over it, but the order is not guaranteed.
var tags = new HashSet<string> { "c#", "dotnet", "c#" };
foreach (var t in tags)
{
Console.WriteLine(t);
}Nested Iteration
You can place one foreach inside another to iterate a collection of collections.
This loops over each row, then each value in that row.
int[][] grid = { new[] {1, 2}, new[] {3, 4} };
foreach (var row in grid)
{
foreach (var v in row)
{
Console.WriteLine(v);
}
}Building a Result
A common pattern is to iterate one collection and add transformed items to another.
Here we double each number and store the result in a new list.
var nums = new List<int> { 1, 2, 3 };
var doubled = new List<int>();
foreach (var n in nums)
{
doubled.Add(n * 2);
}
Console.WriteLine(string.Join(",", doubled));Empty Collections
If a collection has no elements, the foreach body simply never runs.
No error occurs. This makes foreach safe even when a list might be empty.
var empty = new List<int>();
foreach (var x in empty)
{
Console.WriteLine("never printed");
}
Console.WriteLine("done");IEnumerable: The Common Thread
Every iterable collection implements IEnumerable<T>.
That is the contract foreach relies on. Because of it, the same loop syntax works across arrays, lists, sets, and dictionaries.
Quick Check
Test your understanding of iterating collections.
Recap
You saw how foreach works across arrays, lists, dictionaries, and sets.
They share the IEnumerable<T> contract, so one loop style fits them all. Empty collections are safe, and dictionaries yield key-value pairs.
Frequently asked questions
Is the “Iterating Collections” lesson free?
Yes — the full text of “Iterating Collections” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Iterating Collections”?
Lists, arrays, dictionaries. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Iterating Collections” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The foreach Loop
- Iterating Collections
- break and continue
- Common Iteration Mistakes