foreach loop

foreach loop is for the enumerable object. An array is an enumerable object.

The following code uses the foreach to print out the value for each element in an array.


using System;

class Program
{
    static void Main(string[] args)
    {
        int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };

        foreach (int i in intArray)
        {
            Console.WriteLine(i);
        }
    }
}

The output:


1
2
3
4
5
6
7
8
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.