foreach Loops

A foreach loop enables you to address each element in an array using this simple syntax:
foreach ( < baseType > < name > in < array > )
{
// can use < name > for each element
}

This loop will cycle through each element, placing each one in the variable < name > in turn, without danger of accessing illegal elements. You don ’ t have to worry about how many elements are in the array, and you can be sure that you ’ ll get to use each one in the loop.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myIntArray = { 5, 9, 10, 2, 99 };

            foreach (int testVar in myIntArray)
            {
                Console.WriteLine(testVar);
            }
            Console.ReadKey();
        }
    }
}