Table of contents
1.
Introduction
2.
About C# Multidimensional Array
3.
Two-Dimensional Arrays
3.1.
Creating a 2-D Array
3.2.
Accessing the elements in a 2-D array
3.3.
Changing the Elements 
3.4.
Looping through a 2-D Array
4.
Types of 2-D Arrays
4.1.
Rectangular Arrays in C#
4.2.
Jagged Arrays in C#
5.
Frequently Asked Questions
5.1.
What are the uses of multidimensional arrays?
5.2.
How to create a Multidimensional array in C#?
5.3.
How many dimensions can we add in C# Multidimensional Arrays?
5.4.
What are the types of 2-D arrays in C#?
6.
Conclusion
Last Updated: Mar 27, 2024
Hard

Multidimensional Array in C#

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello, Ninjas!

Have you been working on your C# skills lately? Are you looking for a detailed explanation of the C# Multidimensional Array? Well, we got you covered. In this blog, we will be looking at the C# Multidimensional Array and its implementation.

Introduction to C# Multidimensional Arrays

So, let’s dive into C# Multidimensional Array without wasting time.

About C# Multidimensional Array

C# Multi-dimensional arrays are supported up to 32 dimensions. We can declare the C# multidimensional arrays by adding commas in the square brackets. For instance, [,] declares a two-dimensional array, [ , , ] declares a 3-D array, [, , ,] declares a 4-D array, and so on.

So, we can say that -

No. Of commas = Dimensions - 1
 

Declaration

int [ , ] array_2D = new int [4,4]; //Declaring two-dimensional array
int [ , , ] array_3D = new int [3,3,3]; //Declaring three-dimensional array
int [ , , , ] array_4D= new int [4,4,4,4]; //Declaring four-dimensional array
int [ , , , , ] array_5D = new int [3,3,3,3,3]; //Declaring five-dimensional array

Two-Dimensional Arrays

A two-dimensional array is the most basic form of C# multidimensional array. A two-dimensional array can be imagined as a list of one-dimensional arrays. Let us try to visualize it with the help of the following 4x3 2-D array.

2-D Array

The elements have been represented by a[i][j], where i is the row number, and j is the column number.

Creating a 2-D Array

To create a 2-D array in C#, add every 1-D array within a set of curly braces, and insert a comma (,) inside the square brackets.

For instance,

int[,] nums = {{3,4,5},{7,54,67},{9,45,23}};

Let’s visualize the array we just created.

Creating 2-D Array

Accessing the elements in a 2-D array

You must specify the indices to access the elements in a C# multidimensional array. So, in the case of 2-D arrays, you will have to specify two indices. One index is for the array, and the other is for the element inside the array. In simple words, we can also say that one index is for the row number, and the other represents the column number.

Taking the array we created above, let us try to access its elements.

Code:

using System;
class Example2D_array {
  static void Main() {
      
    int[,] nums = {{3,4,5},{7,54,67},{9,45,23}};
    Console.WriteLine(nums[0,1]);
    Console.WriteLine(nums[1,2]);
    Console.WriteLine(nums[2,1]);
    
  }
}

Output:

Output for accessing

Changing the Elements 

In C# Multidimensional arrays, we can also change the value of an element. Let us continue with the previous example and change the element in Row 1 Column 2.

Code:

using System;
class Example2D_array {
  static void Main() {
      
    int[,] nums = {{3,4,5},{7,54,67},{9,45,23}};
    Console.WriteLine("The element before changing:");
    Console.WriteLine(nums[1,2]);
    nums[1,2] = 10;
    Console.WriteLine("The element after changing:");
    Console.WriteLine(nums[1,2]);
    
  }
}

 

Output:

Output for changing element

Looping through a 2-D Array

We can easily loop through elements of a C# 2-D array. Let us try to understand this with the help of the following examples:

Example 1: foreach loop

using System;
class Example2D_array_foreach
{
  static void Main ()
  {


    int[,] nums = { {3, 4, 5}, {7, 54, 67}, {9, 45, 23} };
    Console.WriteLine("The elements in the 2-D array are:");
    foreach (int i in nums)
    {


        Console.WriteLine(i);
    }


  }
}

Output:

foreach loop output

Example 2: for loop

using System;
class Example2D_array_forloop
{
  static void Main ()
  {


    int[,] nums = { {3, 4, 5}, {7, 54, 67}, {9, 45, 23} };
    Console.WriteLine("The elements in the 2-D array are:");
    for (int i = 0; i < nums.GetLength(0); i++) 
    { 
        for (int j = 0; j < nums.GetLength(1); j++) 
        { 
            Console.WriteLine(nums[i, j]); 
        } 
    }  
  }
}

 

Output:

for loop output

Types of 2-D Arrays

The two-dimensional array, interchangeably called a C# multidimensional array, has two types-

  1. Rectangular Array
  2. Jagged Array

Let us see them in detail.

Rectangular Arrays in C#

Rectangle arrays are arrays where rows and columns are equal in number. The creation, accessing, changing, and looping of elements is similar to what has been explained above.

Syntax:

<type>[,] <name> = new <type> [row, column];
Or 
<type>[,] <name> = {list of values}

Jagged Arrays in C#

The data will be stored in these two-dimensional arrays in the form of rows and columns. However, the column size will vary from row to row in this ragged array. Accordingly, if the first row has five columns, the second row may have four, and the third row may have ten. The most important thing to remember is that a jagged array is one in which the column size fluctuates from row to row. It is a rectangular two-dimensional array if the column size is constant across all rows.

The array of arrays is another name for the jagged array in C#. This is because each row in the jagged array is a single-dimensional array. In C#, a jagged array is created by combining many single-dimensional arrays with various column sizes.

Syntax:

<type> [][] <name> = new <type> [row][];
Or
<type> [][] <name> = {list of values}

As you can see, during the declaration of a jagged array, we must specify the number of rows we want in the array.

Let us understand creating, initializing, accessing, and looping jagged array with the help of the following example:
 

Example:

using System;
namespace JaggedArray_example
{
    class example
    {
        static void Main(string[] args)
        {
            //Creating jagged array with 5 Rows
            int[][] arr = new int[5][];


            //Initializing each row with a different column size


            arr[0] = new int[5];
            arr[1] = new int[4];
            arr[2] = new int[3];
            arr[3] = new int[4];
            arr[4] = new int[6];


            //using nested for loop to print the values of Jagged array 
            
            //GetLength(0): This returns the Size of the Rows that is 5
            Console.WriteLine("The default values in Jagged Array:");
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                //arr[i].Length: Returns the Length of Each Row
                for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.Write(arr[i][j] + " ");
                }
            }
            
            //Assigning values to the Jagged array 
            //We use for loop to do this
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                int num = 20;
                for (int j = 0; j < arr[i].Length; j++)
                {
                    num++;
                    arr[i][j] = num;
                }
            }


            //Printing the values of Jagged array 
            //To do this we use foreach loop within the for loop
            
            Console.WriteLine("\n\nThe new values in Jagged Array are:");
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                foreach (int a in arr[i])
                {
                    Console.Write(a + " ");
                }
            }
            
            //Accessing element in the 4th row 2nd column
            Console.WriteLine("\n\nThe element in the 4th row 2nd column is:");
            Console.WriteLine(arr[4][2]);


            
            Console.ReadKey();
        }
    }
} 

Output:

Jagged Array output

 

Frequently Asked Questions

What are the uses of multidimensional arrays?

Data is widely stored in multi-dimensional arrays, an expanded variant of one-dimensional arrays, for use in mathematical computations, image processing, and record management.

How to create a Multidimensional array in C#?

The commas must be placed inside the square brackets to form a C# multidimensional array depending upon the dimensions required.

How many dimensions can we add in C# Multidimensional Arrays?

C# language supports multidimensional arrays up to 32 dimensions.

What are the types of 2-D arrays in C#?

There are two types of 2-D arrays in C#: Rectangular and Jagged.

Conclusion

In this article, we saw the C# Multidimensional arrays in detail. We started with a basic understanding of multidimensional arrays and then proceeded with 2-D arrays and their types. We also saw how to create, initialize, access and loop through arrays. 

You can also check out multidimensional arrays in java and multidimensional arrays in C++.

To explore more interesting coding-related articles, refer to the following links:

 

Recommended problems -

 

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Keep learning, and Keep Growing!

Happy Coding!

Live masterclass