The foreach Statement






The foreach Statement

The foreach statement allows you to iterate through all the items in an array or other collection, examining each item in turn. The syntax for the foreach statement is:

    foreach (type identifier in expression) statement

You can update Figure to replace the final two for statements (that iterate over the contents of the populated array) with foreach statements, as shown in Figure.

Using foreach

foreach ( int i in intArray )
{
   Console.WriteLine( i.ToString( ) );
}
foreach ( Employee e in empArray )
{
   Console.WriteLine( e.ToString( ) );
}

The output will be identical.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows