These are special types of arrays. It is very useful when you want to deal with the data that is grid-like in nature.

Declaring and initializing the two dimensional array is simple. We have to put “,” inside the square bracket. One comma between the square bracket indicates 2D array. Two commas between the square bracket indicates 3D array and so on. This below syntax is very important when declaring the multidimensional arrays.

Declaration of 2D array

Declaring and initializing an array with 4 rows and 4 columns

Declaration of 3D array

We rarely use 3 dimensional arrays in real life programming.

Example program for 2D:

In this program we going to store the values greater than 9 in sequential order.    

    static void Main(string[] args)
    {
        int row = 3, col = 3;

        //2D array declaration and initialization
        int[,] arr = new int[row,col];

        int temp = 10;

        //storing the values in 2d array
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                arr[i,j] = temp++;
            }
        }

        //printing the 2d array values
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                Console.Write(arr[i, j] + "  ");
            }
            Console.WriteLine();
        }
    }
 
      

We got the output as,

10 11 12

13 14 15

16 17 18

I hope you noticed above, arr[i,j] = temp++; We are storing the values in correct row and column positions, and incrementing the temp array to get incremental value. In our case temp would be 10, 11, 12, 13, 14, 15, 16, 17, 18.

From this example, we can clearly understand how to declare and initialize the two dimensional arrays. The first for loop runs from 0th to 2nd index. Second also runs from 0th to 2nd index.

First for loop takes the responsibility of rows. Second for loop takes the responsibility of column.

In the above program,I showed you the dynamic way of storing the values in 2D array. Below is the static way of storing the values in 2D array.

GetLength()

GetLength() is the property of 2D array. In the above code, I have used row and column local variables. Here we going to discuss how to use the GetLength() property to improve our code productivity

To get row height of the 2D array, you can use GetLength(0)

To get column width of the 2D array, you can use GetLength(1)

//printing the 2d array values
for(int i=0;i<arr.GetLength(0);i++)
{
for(int j=0;j<arr.GetLength(1);j++)
{
Console.Write(arr[i, j] + ” “);
}
Console.WriteLine();
}

GetLowerBound() and GetUpperBound()

Similar to GetLength() property, GetLowerBound() and GetUpperBound() are the good properties in C#.

GetLowerBound() returns the lowest index of the array.

GetUpperBound() returns the highest index of the array.

In our example program, GetLowerBound(0) of row returns the index 0. GetUpperBound(0) of row return the index 2. Similarly, GetLowerBound(1) of column returns the index 0. GetUpperBound(1) of column return the index 2.

//printing the 2d array values
for(int i=arr.GetLowerBound(0);i<=arr.GetUpperBound(0);i++)
{
for(int j= arr.GetLowerBound(1);j<=arr. GetUpperBound(1);j++)
{
Console.Write(arr[i, j] + ” “);
}
Console.WriteLine();
}

Though it looks little complicated, it works for non-zero indexed array as well.

Jagged Arrays

This is alternative to multidimensional arrays. It gives the more flexibility at the cost of more complicated syntax. Jagged arrays can also be generalized to other collection types. Both multidimensional and jagged arrays are the techniques that you can use when your data is more complex than a simple list.

Jagged Array is conceptually different from multi-dimensional arrays. The concept of Jagged array is an array of arrays. Each element is an array.

Declaration of Jagged Array

Declaration of Jagged Array

Initialization of Jagged Array

Initialization of Jagged Array

// Traversing the jagged array elements
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
Console.Write(arr[i][j]+” “);
}
Console.WriteLine();
}

We can also initialize the jagged array upon declaration like below,

Thanks for reading, I believe you clearly understood about multidimensional and jagged arrays in C#. If you like this article, please share with your friends.

2 thoughts on “Multidimensional Arrays in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s