C# Multidimensional Arrays

C# allows multi-dimensional arrays. This could be of 2D or 3D (two-dimensional or three-dimensional) Array. These arrays are stored in the form of a table (with rows and columns) which is also known as a matrix.

In C#, multidimensional array is also known as rectangular arrays.

To declare an array of multi-dimensions, we need to use comma (,) inside the brackets []. Such as:

//Two Dimensional array (2D)
int arr[,];

//Three Dimensional array (3D)
int arr[, ,];

Initialization of Multi Dimensional Array in C#

We can initialize a Multi-Dimensional Array (two-dimensional or three-dimensional) in the following ways in C#.

1. Initialization of two-dimensional array:

2D array is the simplest of multi-dimensional array. It has rows and column like a table.

The following is the 2 by 3 matrix array that is array with 2 rows and 3 columns.

int [,] arr = new int [2,3] {
   {0, 1, 2} ,   
   {3, 4, 5}     
};

2. Initialization of three-dimensional array:

int[,, ] arr= new int[2, 2, 3]
{  {
	{ 1, 2, 3 },
	{ 4, 5, 6 }  },
   {
	{ 7, 8, 9 },
	{ 10, 11, 12} }
};

You can also declare and initialize multi-dimensional array in three different ways .

//Method 1
int[,] arr = new int[2,2]= { { 1, 2 }, { 3, 4 } };  

//Method 2: Omitting the array size 
int[,] arr = new int[,]{ { 1, 2 }, { 3, 4 } };  

//Method 3 Omitting new operator
int[,] arr = { { 1, 2 }, { 3, 4 } };

Accessing two Dimensional array in C#

Accessing 2d Array:

Subscripts are used to access the 2D array element. It can be achieved by placing the particular index number within the two brackets [][] (that is the row index and column index). Such as:

arr[2,3]; //accessing in 4D
OR
int val = arr[2,3]; // assigning the element to val

Accessing 3d Array:

Accessing a 3-dimensional array is also as same as a two-dimensional array, the only difference is that in a 3d array we have to specify 3 subscripts with a required index number.

//Accessing 3D Array
arr[2,1,1]; 

Example of C# Multi Dimensional Array:

Example: Two Dimensional Array in C#

C# Program to display all elements in a 2D array. We need to use a nested loop in order to iterate through a multi-dimensional array.

using System;

namespace Arrays
{
   class Program2D
   {
      static void Main(string[] args)
      {
         // array with 4 rows and 3 columns
         int[, ] arr = new int[4, 3]
         {
            { 0, 1, 2 },
            { 3, 4, 5 },
            { 6, 7, 8 },
            { 9, 10, 11 }
         };

         //displaying
         for (int i = 0; i < 4; i++)
         {
            for (int j = 0; j < 3; j++)
            {
               Console.WriteLine("arr[{0},{1}] = {2}", i, j, arr[i, j]);
            }
         }
      }
   }
}

Output:

arr[0,0] = 0
arr[0,1] = 1
arr[0,2] = 2
arr[1,0] = 3
arr[1,1] = 4
arr[1,2] = 5
arr[2,0] = 6
arr[2,1] = 7
arr[2,2] = 8
arr[3,0] = 9
arr[3,1] = 10
arr[3,2] = 11

Example: Three Dimensional Array in C#

C# Program to display all elements in an 3D array

using System;

namespace Arrays
{
   class Program3D
   {
      static void Main(string[] args)
      {
         // 3D array with 2,2,3 dimension
         int[,, ] arr = new int[2, 2, 3]
         {
            {
               { 1, 2, 3 },
               { 4, 5, 6 }
            },
            {
               { 7, 8, 9 },
               { 10, 11, 12 }
            }
         };

         //displaying
         for (int i = 0; i < 2; i++)
         {
            for (int j = 0; j < 2; j++)
            {
               for (int k = 0; k < 3; k++)
               {
                  Console.WriteLine("arr[{0},{1},{2}] = {3}", i, j, k, arr[i, j, k]);
               }
            }
         }
      }
   }
}

Output:

arr[0,0,0] = 1
arr[0,0,1] = 2
arr[0,0,2] = 3
arr[0,1,0] = 4
arr[0,1,1] = 5
arr[0,1,2] = 6
arr[1,0,0] = 7
arr[1,0,1] = 8
arr[1,0,2] = 9
arr[1,1,0] = 10
arr[1,1,1] = 11
arr[1,1,2] = 12

As we saw the examples of 2D and 3D arrays, in a similar we can create any number dimension as required. However, the most used multidimensional array is a two-dimensional array.