When we want to store the information in the format of rows and columns then we use the concept of multidimensional array. Best example of multidimensional array is Microsoft Excel in which all the information is stored in the format of rows and columns. Whenever you want to solve some mathematical problems like matrix, 3-d, 4-d, n-d problems then we also use the concept of multidimensional array. One thing more interesting all the monitors, TVs and LCD’s uses the concept of multidimensional array.
Syntax for Declaring Multidimensional array
<data type> [,] <array name>=new <data type>[<rows>,<columns>];
<data type> represents the data type such as int, float, string etc.
[,]represents dimensions. As much dimensions you wants you have to place so many ,(commos).
[<rows>,<columns>] represents how many rows and columns you need.
Examples for declaring multidimensional array
- int [,] mat=new int[3,5]; //A multidimensional array mat is declared which have two dimensions.
- int [, ,] threeD=new int [3,2,4]; //A multidimensional array threeD is declared which have three dimensions.
Inserting Values In Multidimensional Arrays
After declaring multidimensional arrays we have to insert values in multidimensional arrays. In multidimensional arrays each cell is identified by multiple indexes. If we created two dimensional arrays then each cell is identified by two indexes and if we created three dimensions then each cell is identified by three indexes.
Syntax for inserting values in multidimensional array
<array name>[<row index>,<column index>]=<value>;
Where <row index> represents row number and <column index> represents column number.
Example for inserting values in multidimensional arrays
- mat[0,2]=45; // 45 is stored in mat named array at 0th rows of 2nd columns.
- threeD[1,2,1]=66; // 66 is stored in threeD named array.
Inserting/Traversing of array by using for loops
Example 1: Inserting values in multidimensional array.
public void insertMultiDimensionalArray()
int[,] mat = new int[3, 3];//A multidimensional array of named mat is declared.
for (int i = 0; i < mat.GetLength(0); i++)//outer loop which iterates rows.GetLength(int Dimension) is method of array which gives the length of dimensions which is passed.
for (int j = 0; j < mat.GetLength(1); j++)//inner loop which iterates columns.GetLength(int Dimension) is method of array which gives the length of dimensions which is passed.
Console.Write("Enter Value In Array---> ");//A message is declared.
mat[i, j] = Convert.ToInt32(Console.ReadLine());//Values is stored in multidimensional array
Example 2: Traversing values in multidimensional array.
public void traverseMultiDimensionalArray(int[,] mat)
for (int i = 0; i < mat.GetLength(0); i++)
for (int j = 0; j < mat.GetLength(1); j++)
Console.WriteLine("The values are : " + mat[i, j]);
Note:When use concept of multidimensional array then call GetLength(int Dimension) method of array which returns length of the dimensions. Never use Length property of array.