Loop through a two-dimensional array with foreach loop in CSharp
Description
The following code shows how to loop through a two-dimensional array with foreach loop.
Example
using System; /*w ww . ja v a 2 s. c o m*/
public class MainClass {
public static void Main() {
int sum = 0;
int[,] nums = new int[3,5];
for(int i = 0; i < 3; i++)
for(int j=0; j < 5; j++)
nums[i,j] = (i+1)*(j+1);
foreach(int x in nums) {
Console.WriteLine("Value is: " + x);
sum += x;
}
Console.WriteLine("Summation: " + sum);
}
}
The code above generates the following result.