Search This Blog

Thursday, October 7, 2010

c# - MulitDimensionla Array With Foreach

Example with a Rectangular Array



The following example shows the foreach statement used with a rectangular array:


class Sample
{
static void Main()

{
int nTotal = 0;
int[,] arr1 = { {10, 11}, {12, 13} };

 
foreach( int element in arr1 )
{
nTotal += element;
Console.WriteLine

("Element: {0}, Current Total: {1}",
element, nTotal);
}
}
}

The output is the following:





Element: 10, Current Total: 10

Element: 11, Current Total: 21

Element: 12, Current Total: 33

Element: 13, Current Total: 46

No comments:

Post a Comment