Start learning free

LESSON 105 OF 347 · FOR LOOPS - LOOPING OVER ARRAYS

Looping Through Arrays in C#: The foreach Loop

Lesson Read the full explanation

A for loop is a special type of loop in C# which allows us to loop over the items of a collection, such as an array.


Looping through an array:

For example, an array is a sequence of items, so we can use a for loop to iterate over each item and do something with it:

string[] fruits = {"banana", "orange", "pear", "kivi"};

for (string fruit : fruits) {
  Console.WriteLine("I eat " + fruit);
}

The output of this code is:

I eat banana
I eat orange
I eat pear
I eat kivi

Let's break down this code:

  • The for keyword that indicates the start of a for loop.
  • string fruit is the loop variable. It will take the value of each different item in the collection
  • The : separates the loop variable from the collection we will iterate on
  • fruits is the collection to loop over

Assignment
Now let's greet our friends using a for loop!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    C# · FOR LOOPS - LOOPING OVER ARRAYS

    Looping Through Arrays in C#, explained

    Why loop over an array

    An array holds a sequence of items, and a very common job is to do something with each one: print it, add it to a total, check it against a condition. A loop lets you write that action once and run it for every item. C# has several loops. For visiting every element of an array in order, the cleanest is foreach.

    The foreach loop

    string[] fruits = {"banana", "orange", "pear", "kiwi"};
    
    foreach (string fruit in fruits) {
        Console.WriteLine("I eat " + fruit);
    }
    

    The output is:

    I eat banana
    I eat orange
    I eat pear
    I eat kiwi
    

    The loop header has four parts:

    • foreach starts the loop.
    • string fruit is the loop variable. It holds a different element on each pass: first "banana", then "orange", and so on.
    • in separates the loop variable from the collection. C# uses the word in here, not a colon.
    • fruits is the collection to walk through.

    The statements between the braces are the loop body. It runs once per element, and the loop ends after the last one. You never write an index, so there is no counter to start, increase or compare against the length.

    foreach versus for

    In C#, the keyword for is the counting loop. It is the right choice when you need the index itself:

    for (int i = 0; i < fruits.Length; i++) {
        Console.WriteLine(i + ": " + fruits[i]);
    }
    // 0: banana
    // 1: orange
    // 2: pear
    // 3: kiwi
    

    Note i < fruits.Length: the last valid index is Length - 1, so a condition of i <= fruits.Length would read one element past the end and throw an IndexOutOfRangeException. When you only need the values, foreach is shorter and removes that risk.

    A syntax from other languages sometimes appears in ported code: for (string fruit : fruits). That is not valid C#. The loop over elements is foreach (string fruit in fruits).

    What you can and can't do in the loop

    • Type. The loop variable's type must fit the elements. You can write var and let the compiler infer it: foreach (var fruit in fruits).
    • The loop variable is read-only. Assigning to it, as in fruit = "x";, is a compile error. It is a copy of the element for that pass and doesn't write back into the array.
    • Empty arrays are fine. The body simply never runs.
    • Other collections work too. foreach also walks a List<T>, the characters of a string and many other collections. Changing the size of a list while you loop over it throws an InvalidOperationException.

    The exercise, step by step

    The lesson gives you an array of friends and asks you to greet each one.

    1. Write the loop. Use foreach with a string variable named friend, looping through the friends array. Open a brace for the body.
    2. Print the greeting inside the body. For each friend, call Console.WriteLine("Hey, " + friend + "!"). The + operator joins strings, and WriteLine adds the line break after each greeting.
    string[] friends = {"Andy", "Mary", "John"};
    
    foreach (string friend in friends) {
        Console.WriteLine("Hey, " + friend + "!");
    }
    

    It prints:

    Hey, Andy!
    Hey, Mary!
    Hey, John!
    

    Common mistakes

    • Using a colon instead of in. foreach (string friend : friends) doesn't compile.
    • Using the wrong variable inside the body. The loop variable's name is the one in the header. Writing friends in the message would try to join a whole array into the text.
    • Printing outside the braces. A statement after the closing brace runs once, after the loop is finished. Without braces, a foreach runs only the single statement right after it, so always use braces.
    • Forgetting the space or punctuation in the text. "Hey, " has a space after the comma, and the exclamation mark is part of the expected output.
    • Trying to change elements through the loop variable. To modify an array in place, use a for loop with an index.

    Key takeaways

    • foreach (Type item in collection) { ... } runs the body once for each element, in order.
    • The loop variable takes each element's value and is read-only.
    • Use for with an index when you need positions or want to change elements.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free