foreach Loop

noun

say it: “for-EECH loop” /fɔːrˈiːtʃ luːp/

A C# loop that visits every item in a collection, one at a time, without an index.

What is foreach Loop in C#?

foreach Loop — A loop that visits each item in a collection or sequence.

class PetRegistry
{
    int CountNames(string[] pets)
    {
        int count = 0;

        foreach (string pet in pets)
        {
            count++;
        }

        return count;
    }
}

In a sentence

  • “A foreach loop prints every title in the reading list.”

Level: Apprentice · More C# terms at this level

Updated

In other subjects