display all elements from a 2 dimensional array in csharp

To display all elements from a 2 dimensional array in C#, you can use nested loops to iterate through each element of the array and print it to the console.

Here's an example of how to do it:

main.cs
int[,] myArray = new int[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

for (int i = 0; i < myArray.GetLength(0); i++)
{
    for (int j = 0; j < myArray.GetLength(1); j++)
    {
        Console.Write(myArray[i, j] + " ");
    }
    Console.WriteLine();
}
258 chars
11 lines

In this example, we first create a 2 dimensional array called myArray with 3 rows and 3 columns. We then use nested loops to iterate through each element of the array. The outer loop iterates through the rows (i), while the inner loop iterates through the columns (j).

Inside the nested loops, we use Console.Write to print each element of the array to the console. We add a space after each element so that the output is nicely formatted. After printing all the elements in a row, we use Console.WriteLine to move to the next line and start printing the next row.

When we run this code, we'll see the following output:

main.cs
1 2 3
4 5 6
7 8 9
18 chars
4 lines

This displays all the elements of the 2 dimensional array in a nicely formatted way.

gistlibby LogSnag