Multi dimensional arrays in c#

 
We know that array is the combination of the values of the similar data types. If we talk about the dimensions in the arrays, in C# programming Arrays can have one or more dimensions. An Array which has one dimension is called by one dimensional array and when array has more than one dimension called by multidimensional array.
Generally one dimensional array are commonly used in the programming. in this article i will try to multidimensional array.
 

Two dimensional Array

The following example shows how to use two dimensional array to store the data table values.
Suppose we have a following table:
 

MathChecmistryPhysics
Student1505565
Student2604578
Student3706956

 

From the above there are three rows and three columns total nine values.
In C# we can define this table of items by using two dimension array. In C# Two dimension array are stored in memory in these ways:

 

Column1 Column2 Column3

50 [0,0]55 [0,1]65 [0,2]

Row1

60 [1,0]45 [1,1]78 [1,2]

Row2

70 [2,0]69 [2,1]56 [2,2]

Row3

How to declare two dimensional array

You can define two dimensional array like this:

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

you can also use this :

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

Here is the complete example. It loads a two dimensional array with marks of all subjects for each students.

Private void CreateArrayOfMarks()
{
    int arrMarks[,] = new int [3,4];
    arrMarks[0,0] = 50;
    arrMarks[0,1] = 55;
    arrMarks[0,2] = 65;
 
    arrMarks[1,0] = 60; 
    arrMarks[1,1] = 45;
    arrMarks[1,2] = 78;
    arrMarks[2,0] = 70;
    arrMarks[2,1] = 69;
    arrMarks[2,2] = 56;
}

How to access an element

To access an element in a two dimension array, you must specify both indices, separating the two with a comma. For example, to get the marks in chemistry of the student2 , you would use

int Checmistry_marks = arrMarks[1,1];

And the general syntax of the declaring n-dimensional array is:
type [,...., ] arrayname = new type [size1, size2, ...., sizeN]