C# arrays store collection of items of a particular data type. C# Arrays can be one-dimensional where items are stored in the form of a row, or multidimensional where elements are stored in the form of a matrix, a collection of a matrix, and so on, depending on the dimensions of an array. In this article, you will see what are C# multidimensional arrays, how you can create such arrays, how to add items in multidimensional arrays, and how to display items from multidimensional arrays. So, let’s begin without ado.

Table of Contents

  1. One Dimensional Array
  2. Multidimensional Arrays
  3. Adding Items to Multidimensional Arrays
  4. Displaying Items from Multidimensional Arrays

Multidimensional Arrays in C#

One Dimensional Array

Before I explain C# multidimensional arrays, let us briefly review what is a C# one dimensional array. In a one-dimensional array, items are stored in the form of a single row. For example, the following script creates a one-dimensional array named my_array which can store 5 items. Next, the items are stored index wise in the array. C# arrays follow a zero-based index scheme where the first item of an array is stored at 0th index, the second item is stored at 1st index, and so on.

To store an element at a particular index, you need to write the name of the array, followed by a pair of square brackets. Inside the square brackets, you specify the index of the element that you want to access or update. For instance, to access the item at the second index of my_array array, you should use my_array[2] script.  Look at the following script for reference:

using System;

namespace DariaApp
{
class Program
{
static void Main(string[] args)
{

int[] my_array = new int[5];

my_array[0] = 2;
my_array[1] = 5;
my_array[2] = 1;
my_array[3] = 9;
my_array[4] = 8;

foreach(int num in my_array)
{
Console.WriteLine(num);
}

Console.ReadKey();
}
}
}

Output:

Multidimensional Arrays

In simplest words, multidimensional arrays can be defined as arrays of arrays. Depending upon the number of dimensions, multidimensional arrays may look like matrices, or an array of matrices, or an array of collections of matrices, and so on.

For instance, a two-dimensional array looks like a matrix. The following script creates a two-dimensional array my_mul_array2 that contains three rows and 2 columns. To define a variable that stores a two-dimensional array, you have to specify a single comma inside the square brackets. Next, instead of storing a single item inside curly brackets, you store arrays.

using System;

namespace DariaApp
{
class Program
{
static void Main(string[] args)
{


int[,] my_mul_array2 = { 
{14, 5 },
{10, 20},
{6, 90}
};

Console.ReadKey();
}
}
}

You can also create three-dimensional arrays. A three-dimensional array is basically an array of multiple two-dimensional arrays (or matrices). The following script creates a three-dimensional array of the order 2, 2, 3 which means that the array basically contains 2 two-dimensional arrays where each of the two-dimensional arrays contains three rows and two columns.

using System;

namespace DariaApp
{
class Program
{
static void Main(string[] args)
{

int[,,] my_mul_array = {// Array Starts here
{ // Matrix 1
{14, 5 },
{10, 20},
{6, 90}
},

{ // Matrix 2
 { 15, 6 },
{ 25, 35},
{ 9, 110}
}
}; // Array ends here

}
}
}

In the same way, you can create four, five, and larger dimensional arrays. For instance, an array of dimensions [4, 5, 4,3] consists of 4 three-dimensional arrays where each of the three-dimensional arrays contains five two-dimensional arrays where each of the two-dimensional arrays contains four rows and three columns.

Adding Items to Multidimensional Arrays

Adding an item to a multidimensional array is straight-forward. You have to access the index of the item and then equate it to the value that you want to assign to the index. For instance, in the following script, we insert 6 items in the two-dimensional array my_mul_array which consists of 2 rows and 3 columns.

using System;

namespace DariaApp
{
class Program
{
static void Main(string[] args)
{

int[,] my_mul_array = new int[2, 3];

my_mul_array[0,0] = 10; // first row first column
my_mul_array[0,1] = 5;
my_mul_array[0,2] = 25;
my_mul_array[1,0] = 32; // second row first column
my_mul_array[1,1] = 12;
my_mul_array[1,2] = 18; // second row third column

}
}
}

In the same way, you can add items to a three-dimensional array. For instance, in the following script, you update the item in the matrix at index 1, and inside the matrix, the row at index 2 (third row) and column at index 0 (first column). You will see that the value 9 will be updated to 999.

using System;

namespace DariaApp
{
class Program
{
static void Main(string[] args)
{

int[,,] my_mul_array = {// Array Starts here
{ // Matrix 1
{14, 5 },
{10, 20},
{6, 90}
},

{ // Matrix 2
{ 15, 6 },
{ 25, 35},
{ 9, 110}
}
}; // Array ends here

// item in second matrix, third row, first column
my_mul_array[1, 2, 0] = 999; ;
}
}
}

Displaying Items from Multidimensional Arrays

There are two ways to display items from multidimensional arrays. You can either display a single item or you can display all the items. To display a single item, all you have to do is access the item using its index and then print the item on the console.

To display all the items in an array, you need N for loops where N is the dimension of the array that you want to print items from. For instance, the following script prints a single item from a two-dimensional array and then all the items from the array. To print all the items, you need to execute two nested for loops. The outer for loop iterates through rows. The internal for loop iterates through items within each array that is being iterated by the outer for loop.

using System;

namespace DariaApp
{
class Program
{
static void Main(string[] args)
{


int[,] my_mul_array2 = {
{14, 5 },
{10, 20},
{6, 90}
};


//printing single item

Console.WriteLine(my_mul_array2[1, 1]); // item in second row second column
Console.WriteLine("======= ");

// printing all items
for (int i = 0; i < my_mul_array2.GetLength(0); i++)
{
for (int j = 0; j < my_mul_array2.GetLength(1); j++)
{
Console.Write(my_mul_array2[i, j] + " ");

}
Console.WriteLine("");
}

Console.ReadKey();
}
}
}

Output:

The output below shows a single as well as all the items from the my_mul_array.

 

In the same way, you can iterate through a single item in a three-dimensional array and all the items in a three-dimensional array. To access a single item, you simply have to access its index using square bracket notation. To display all the items, you need three for loops, one per dimension. Look at the following script for reference.

using System;

namespace DariaApp
{
class Program
{
static void Main(string[] args)
{

int[,,] my_mul_array = {// Array Starts here
{ // Matrix 1
{14, 5 },
{10, 20},
{6, 90}
},

{ // Matrix 2
{ 15, 6 },
{ 25, 35},
{ 9, 110}
}
}; // Array ends here

// item in second matrix, third row, first column
my_mul_array[1, 2, 0] = 999;

//printing single item

Console.WriteLine(my_mul_array[1, 2, 0]);
Console.WriteLine(" ======= ");

// printing all items
for (int i = 0; i < my_mul_array.GetLength(0); i ++)
{
for (int j = 0; j < my_mul_array.GetLength(1); j++)
{
for (int k = 0; k < my_mul_array.GetLength(2); k++)
{
Console.Write(my_mul_array[i, j, k] + " ");
}
Console.WriteLine("");
}
Console.WriteLine("");
}
}
}
}

From the output, you see a single value 999 printed on the console. You can also see all the items from the three-dimensional array in the console output.

Output: