Print a 2 D Array or Matrix in C#

A 2D array or matrix in C# is a data structure that stores elements in rows and columns. To print a 2D array, you need to use nested loops to iterate through each row and column, displaying the elements in a formatted manner.

Syntax

Following is the syntax for declaring a 2D array −

dataType[,] arrayName = new dataType[rows, columns];

Following is the syntax for printing a 2D array using nested loops −

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < columns; j++) {
        Console.Write(array[i, j] + "\t");
    }
    Console.WriteLine();
}

Using Predefined Values

The simplest way to print a 2D array is to initialize it with predefined values and then display them using nested loops −

using System;

class Program {
    static void Main() {
        int[,] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        Console.WriteLine("Matrix:");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                Console.Write(matrix[i, j] + "\t");
            }
            Console.WriteLine();
        }
    }
}

The output of the above code is −

Matrix:
1       2       3
4       5       6
7       8       9

Using GetLength() Method

For dynamic arrays where the size might vary, use the GetLength() method to get the dimensions −

using System;

class Program {
    static void Main() {
        int[,] matrix = {
            {10, 20},
            {30, 40},
            {50, 60}
        };

        Console.WriteLine("Matrix using GetLength():");
        for (int i = 0; i < matrix.GetLength(0); i++) {
            for (int j = 0; j < matrix.GetLength(1); j++) {
                Console.Write(matrix[i, j] + "\t");
            }
            Console.WriteLine();
        }

        Console.WriteLine("Matrix dimensions: " + matrix.GetLength(0) + "x" + matrix.GetLength(1));
    }
}

The output of the above code is −

Matrix using GetLength():
10      20
30      40
50      60
Matrix dimensions: 3x2

Using foreach Loop

You can also use a foreach loop to print all elements, though this doesn't maintain the matrix structure −

using System;

class Program {
    static void Main() {
        int[,] matrix = {
            {1, 2, 3},
            {4, 5, 6}
        };

        Console.WriteLine("Using foreach (linear display):");
        foreach (int element in matrix) {
            Console.Write(element + " ");
        }

        Console.WriteLine("<br>\nUsing nested for loops (matrix format):");
        for (int i = 0; i < matrix.GetLength(0); i++) {
            for (int j = 0; j < matrix.GetLength(1); j++) {
                Console.Write(matrix[i, j] + "\t");
            }
            Console.WriteLine();
        }
    }
}

The output of the above code is −

Using foreach (linear display):
1 2 3 4 5 6 

Using nested for loops (matrix format):
1       2       3
4       5       6

Key Points

  • Use GetLength(0) for the number of rows and GetLength(1) for the number of columns.

  • The outer loop iterates through rows, and the inner loop iterates through columns.

  • Use \t for tab spacing and Console.WriteLine() after each row for proper formatting.

  • A foreach loop prints elements linearly, not in matrix format.

Conclusion

Printing a 2D array in C# requires nested loops to iterate through rows and columns. Use GetLength() for dynamic sizing and proper formatting with tabs and line breaks to display the matrix structure clearly.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements