Reputation: 43
How do you iterate through a 2d array and get the first element in each row? For example:
int[,] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };
desired output: 1 4 7 10
Upvotes: 3
Views: 2129
Reputation: 38785
Something like this should do the trick:
int[,] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };
for (int row = 0; row < array.GetLength(0); ++row)
{
Console.WriteLine(array[row, 0]);
}
Upvotes: 2