C# Loop Through Arrays


Loop Through an Array

You can loop through the array elements with the for loop, and use the Length property to specify how many times the loop should run.

The following example outputs all elements in the cars array:

Example

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.Length; i++) 
{
  Console.WriteLine(cars[i]);
}

Try it Yourself »


The foreach Loop

There is also a foreach loop, which is used exclusively to loop through elements in an array:

Syntax

foreach (type variableName in arrayName) 
{
  // code block to be executed
}

The following example outputs all elements in the cars array, using a foreach loop:

Example

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars) 
{
  Console.WriteLine(i);
}

Try it Yourself »

The example above can be read like this: for each string element (called i - as in index) in cars, print out the value of i.

If you compare the for loop and foreach loop, you will see that the foreach method is easier to write, it does not require a counter (using the Length property), and it is more readable.


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.