Iterating Collections
Lists, arrays, dictionaries.
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);
}All lessons in this course
- The foreach Loop
- Iterating Collections
- break and continue
- Common Iteration Mistakes