Multi-dimensional Array:
We have learned about single dimensional arrays in the previous section. C# also supports multi-dimensional arrays. A multi-dimensional array is a two dimensional series like rows and columns.
Example: Multi-dimensional Array:
int[,] intArray = new int[3,2]{
{1, 2},
{3, 4},
{5, 6}
};
// or
int[,] intArray = { {1, 1}, {1, 2}, {1, 3} };
As you can see in the above example, multi dimensional array is initialized by giving size of rows and columns. [3,2] specifies that array can include 3 rows and 2 columns.
The following figure shows a multi-dimensional array divided into rows and columns:
The values of a multi-dimensional array can be accessed using two indexes. The first index is for the row and the second index is for the column. Both the indexes start from zero.
Example: Access Multi dimensional Array
int[,] intArray = new int[3,2]{
{1, 2},
{3, 4},
{5, 6}
};
intArray[0,0]; //Output: 1
intArray[0,1]; // 2
intArray[1,0]; // 3
intArray[1,1]; // 4
intArray[2,0]; // 5
intArray[2,1]; // 6
In the above example, intArray[2,1] returns 3. Here, 2 means the third row and 1 means the second column (rows and columns starts with zero index).