|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Using foreach with Arrays (C# Programming Guide)
C# also provides the foreach statement. This statement provides a simple, clean way to iterate through the elements of an array. For example, the following code creates an array called numbers and iterates through it with the foreach statement:
With multidimensional arrays, you can use the same method to iterate through the elements, for example:
However, with multidimensional arrays, using a nested for loop gives you more control over the array elements.