Here's an example:
csharp
int[,] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < array.GetLength(0); i++) {
for (int j = 0; j < array.GetLength(1); j++) {
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
`
In this example:
- `array.GetLength(0)` gives the number of rows in the array.
- `array.GetLength(1)` gives the number of columns in each row.
The outer loop (`for (int i = 0; i < array.GetLength(0); i++)`) iterates over each row, and the inner loop (`for (int j = 0; j < array.GetLength(1); j++)`) iterates over each column in that row. The `Console.Write(array[i, j] + " ");` statement prints the value at position `(i, j)` and a space.
After printing all elements in one row, `Console.WriteLine();` is used to move to the next line for readability.
Login to Continue, We will bring you back to this content 0