E_C
E_C

Reputation: 43

Get first element in each row of 2d array

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

Answers (1)

ProgrammingLlama
ProgrammingLlama

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]);
}

Try it online

Upvotes: 2

Related Questions