C# Multi-Dimensional Arrays

In the last lesson we learned about One-Dimensional Arrays in Java and how to use them. Now, we will see another type of arrays in C# i.e. Multidimensional Arrays. Multi-dimensional arrays as the name suggests are arrays of arrays.  These are also called rectangular arrays. Multi-Dimensional can be the following in C#:

  • Two-Dimensional Arrays
  • Three-Dimensional Arrays

Declare and Initialize a Multi-Dimensional Array

To create a Multi-Dimensional array, set a comma inside the square bracket. You need to use two square brackets to declare a multidimensional array.

For a Two-Dimensional array, set a single comma as shown below:

int[,] marks = new int[2,2];

We can also directly declare and initialize a Two-Dimensional array:

int [,] marks = new int [3,3] {
   {90, 75, 86} ,  
   {72, 89, 68} ,
   {58, 99, 93} 
};

For a Three-Dimensional array, set two comma’s as shown below:

int[,,] marks = new int[2,2,2];

The following figure displays how a multidimensional array forms a 2×3 matrix,

Multi-Dimensional Arrays in Csharp

In the above figure, you can see the left index determines row and the right index determines column.

Example – Multi-Dimensional Arrays in C#

Let us now see an example to display each element from our 2×3 matrix,

using System;

namespace Demo
{
  class Studyopedia 
  {
    static void Main(string[] args)
    {     
      int i, j, k = 0; 
      int[,] marks = new int[2,3]; 
      
      for(i=0; i<2; i++) {   
        for(j=0; j<3; j++) {
          marks[i, j] = k;
          k++;      
        }
      }
 
      for(i=0; i<2; i++) {
        for(j=0; j<3; j++)
          Console.WriteLine("marks[{0},{1}] = {2}", i, j, marks[i,j]);
          Console.Write("\n");     
        }
      } 
  }
}

Output

marks[0,0] = 0
marks[0,1] = 1
marks[0,2] = 2

marks[1,0] = 3
marks[1,1] = 4
marks[1,2] = 5

Example – Access elements of a Multi-Dimensional Array in C#

We can easily access the elements of a two-dimensional array. Set the array name and then two indexes i.e.

// Access the 1st row and 1st column
marks[0,0]

// Access the 1st row and 2nd column
marks[0,1]

// Access the 2nd row and 2nd column
marks[1,1]

Let us follow what we saw above in the following example:

using System;

namespace Demo
{
  class Studyopedia 
  {
    static void Main(string[] args)
    {     
      int i, j, k = 0; 
      int [,] marks = new int [3,3] {
        {90, 75, 86} ,  
        {72, 89, 68} ,
        {58, 99, 93} 
      };
      
      // 1st row elements
      Console.WriteLine("Access the 1st row and 1st column = {0}",marks[0,0]);
      Console.WriteLine("Access the 1st row and 2nd column = {0}",marks[0,1]);
      Console.WriteLine("Access the 1st row and 3rd column = {0}",marks[0,2]);
      
      // 2nd row elements
      Console.WriteLine("\nAccess the 2nd row and 1st column = {0}",marks[1,0]);
      Console.WriteLine("Access the 2nd row and 2nd column = {0}",marks[1,1]);
      Console.WriteLine("Access the 2nd row and 3rd column = {0}",marks[1,2]);
      
      // 3rd row elements
      Console.WriteLine("\nAccess the 3rd row and 1st column = {0}",marks[2,0]);
      Console.WriteLine("Access the 3rd row and 2nd column = {0}",marks[2,1]);
      Console.WriteLine("Access the 3rd row and 3rd column = {0}",marks[2,2]);
    }
  }
}

Output

Access the 1st row and 1st column = 90
Access the 1st row and 2nd column = 75
Access the 1st row and 3rd column = 86

Access the 2nd row and 1st column = 72
Access the 2nd row and 2nd column = 89
Access the 2nd row and 3rd column = 68

Access the 3rd row and 1st column = 58
Access the 3rd row and 2nd column = 99
Access the 3rd row and 3rd column = 93
C# Arrays
C# - Loops
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment