Freigeben über


foreach, in (C# Reference)

The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface. The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.

The embedded statements continue to execute for each element in the array or collection. After the iteration has been completed for all the elements in the collection, control is transferred to the next statement following the foreach block.

At any point within the foreach block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword.

A foreach loop can also be exited by the goto, return, or throw statements.

For more information about the foreach keyword and code samples, see the following topics:

Using foreach with Arrays (C# Programming Guide)

How to: Access a Collection Class with foreach (C# Programming Guide)

Example

The following code shows three examples:

  • a typical foreach loop that displays the contents of an array of integers

  • a for loop that does the same thing

  • a foreach loop that maintains a count of the number of elements in the array

class ForEachTest
{
    static void Main(string[] args)
    {
        int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
        foreach (int element in fibarray)
        {
            System.Console.WriteLine(element);
        }
        System.Console.WriteLine();


        // Compare the previous loop to a similar for loop. 
        for (int i = 0; i < fibarray.Length; i++)
        {
            System.Console.WriteLine(fibarray[i]);
        }
        System.Console.WriteLine();


        // You can maintain a count of the elements in the collection. 
        int count = 0;
        foreach (int element in fibarray)
        {
            count += 1;
            System.Console.WriteLine("Element #{0}: {1}", count, element);
        }
        System.Console.WriteLine("Number of elements in the array: {0}", count);
    }
    // Output: 
    // 0 
    // 1 
    // 1 
    // 2 
    // 3 
    // 5 
    // 8 
    // 13 

    // 0 
    // 1 
    // 1 
    // 2 
    // 3 
    // 5 
    // 8 
    // 13 

    // Element #1: 0 
    // Element #2: 1 
    // Element #3: 1 
    // Element #4: 2 
    // Element #5: 3 
    // Element #6: 5 
    // Element #7: 8 
    // Element #8: 13 
    // Number of elements in the array: 8
}

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

Reference

C# Keywords

Iteration Statements (C# Reference)

for (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference