C# Multidimensional Arrays

Summary: in this tutorial, you’ll learn about C# multidimensional arrays, including 2D and 3D arrays.

Introduction to C# multidimensional arrays

In the array tutorial, you learned how to create an array with one dimension. C# allows you to define an array with multiple dimensions.

When an array has more than one dimension, it is called a multidimensional array.

The following example declares a two-dimensional array (or 2D array) of two rows and three columns

int [,] matrix = new int[2, 3];Code language: C# (cs)

The comma (,) inside the square brackets denotes the number of dimensions. If you want to declare a three-dimensional array (or 3D array), you can use two commas (,) like this:

int[,,] tensor = new int[3, 3, 3];Code language: C# (cs)

Initializing C# multidimensional arrays

The following example shows how to initialize a two-dimensional array with two rows and three columns:

int[,] matrix = new int[2, 3] {
    { 1, 2, 3 }, 
    { 4, 5, 6 }, 
};Code language: C# (cs)

Or you can make it shorter but less obvious like this:

int[,] matrix = {
    { 1, 2, 3 }, 
    { 4, 5, 6 }, 
};Code language: C# (cs)

And the following example shows how to initialize an array of three dimensions 2, 2, and 3:

int[,,] tensor = new int[2,2,3] { 
    { { 1, 2, 3 }, { 4, 5, 6 } },
    { { 7, 8, 9 }, { 10, 11, 12 } } 
};Code language: C# (cs)

It’s the same as the following:

int[,,] tensor = { 
    { { 1, 2, 3 }, { 4, 5, 6 } },
    { { 7, 8, 9 }, { 10, 11, 12 } } 
};Code language: C# (cs)

Accessing array elements

To access an element in the multidimensional array, you use a comma-separated list of dimensions inside the square brackets. The first dimension starts at zero.

For example, the following example accesses the row 0 column 1 of a two-dimensional array:

array2D[0,1]Code language: C# (cs)

Similarly, you can specify the dimensions in a 3D array like this:

array3D[0,1,2]Code language: C# (cs)

The following example shows how to access the elements at row 0 column 0 and row 1 and column 2 of a 2D array:

int[,] matrix = {
    {1,2,3},
    {4,5,6},
    {7,8,9},
};

// row 0 column 0
Console.WriteLine(matrix[0, 0]); // 1

// row 1 column 2
Console.WriteLine(matrix[1, 2]); // 6Code language: C# (cs)

Output:

1
6Code language: C# (cs)

Getting dimension length

The GetLength method of an array returns the length of a dimension. The first dimension starts at zero. The following example declares a 2D array, initialize its elements, and output all the elements to the console:

int[,] matrix = new int[3, 3];

// assign integers to the 2D array
for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        matrix[i, j] = i + j;
    }
}


// output the 2D array
for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        Console.Write($"{matrix[i, j]}");
        Console.Write(j < matrix.GetLength(1) - 1 ? "," : "");
    }
    Console.WriteLine();
}
Code language: C# (cs)

Output:

0,1,2
1,2,3
2,3,4Code language: C# (cs)

Summary

  • A multidimensional array is an array with more than on dimension.
Was this tutorial helpful ?