Section 10.2. The foreach Statement


10.2. 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 Example 10-1 to replace the final two for statements (that iterate over the contents of the populated array) with foreach statements, as shown in Example 10-2.

Example 10-2. 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.



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net