# C# Multidimensional and Jagged Arrays

In the previous chapter, we learned about a single dimensional array. We can also have multidimensional arrays in C#. Suppose there are 50 students in a class and each student is studying 5 subjects. We can make a two-dimensional array to store marks of each student in each subject.

## C# Multidimensional Array

Let's talk about a 2D array first which is also known as matrix and consist of rows and columns.

Let's first see how to declare and initialize a 2D array.

int[,] a = new int[2,4];

Here, a is a 2D array of type int which consists of 2 rows and 4 columns.

It is like

Column 0 Column 1 Column 2 Column 3
Row 0 a[0, 0] a[0, 1] a[0, 2] a[0, 3]
Row 1 a[1, 0] a[1, 1] a[1, 2] a[1, 3]

Now let's see how to initialize a 2-dimensional array.

We can assign values to a 2-dimensional array in following ways:

int[,] a = new int[2,4] { {1, 2, 3, 4}, {5, 6, 7, 8} };

or

int[,] a = new int[,] { {1, 2, 3, 4}, {5, 6, 7, 8} };

or

int[,] a = { {1, 2, 3, 4}, {5, 6, 7, 8} };

We can access elements of a 2D array as - arrayName[row, column]. Using this, we can also set the values of the array differently as we do with a single dimensional array.

Suppose we declared a 2-dimensional array a[2, 2]. Then to assign it values, we can assign values to its elements.

int[,] a = new int[2, 2];
a[0, 0] = 1;
a[0, 1] = 2;
a[1, 0] = 3;
a[1, 1] = 4;

Let's look at an example.

using System;

class Test
{
static void Display(int[,] a)
{
foreach(int i in a)
Console.Write($"{i}\t"); Console.Write("\n"); } static void Main(string[] args) { int[,] a = new int[2, 2]; a[0, 0] = 1; a[0, 1] = 2; a[1, 0] = 3; a[1, 1] = 4; int[,] b = new int[2, 2] { {5, 6}, {7, 8} }; int[,] c = { {9, 10}, {11, 12} }; foreach(int i in a) Console.Write($"{i}\t");
Console.Write("\n");

foreach(int i in b)
Console.Write($"{i}\t"); Console.Write("\n"); foreach(int i in c) Console.Write($"{i}\t");
Console.Write("\n");
}
}

1  2  3  4
5  6  7  8
9  10  11  12

### C# Passing Multidimensional Array to Method

We can also pass a multidimensional array to a method. For example, to pass a 2D array of integers to a method Abc, we will write - Abc(int[,] a).

Let's look at an example.

using System;

class Test
{
static void Display(int[,] a)
{
foreach(int i in a)
Console.Write($"{i}\t"); Console.Write("\n"); } static void Main(string[] args) { int[,] a = new int[2, 2]; a[0, 0] = 1; a[0, 1] = 2; a[1, 0] = 3; a[1, 1] = 4; int[,] b = new int[2, 2] { {5, 6}, {7, 8} }; int[,] c = { {9, 10}, {11, 12} }; Display(a); Display(b); Display(c); } }  1 2 3 4 5 6 7 8 9 10 11 12 Here, the Display method is taking a 2D array of integers - Display(int[,] a). Similarly, we can have 3D array as: int[, ,] a = new int[5,6,7]; 4D array as int[, , ,] a = new int[5,6,7,3]; and so on. ### C# Returning Multidimensional Array from Method To return a 2D array from a method, we will specify its return type as [,]. For example, to return a 2D array from a function Abc, we would specify - int[,] Abc(). Let's look at an example. using System; class Test { static int[,] MultiReturn() { int[,] a = new int[2, 2] { {5, 6}, {7, 8} }; return a; } static void Main(string[] args) { int[,] a = MultiReturn(); foreach(int i in a) Console.Write($"{i}\t");
Console.Write("\n");
}
}

5  6  7  8

Similarly, we can use [, ,] for a 3D array, [, , ,] for a 4D array and so on.

## C# Jagged Array

A jagged array is basically an array of arrays. So, it is like the following picture.

We declare a jagged array as:

int[][] a = new int[2][];

Here, it means that the jagged array a will contain 2 arrays inside it.

As the jagged array is an array of arrays, its individual element will be a different array.

We can assign arrays to a jagged array in different ways. Let's assign arrays to the jagged array a:

a[0] = new int[2];
a[1] = new int[3];

We have a jagged array a and now its first element is an array of 2 integers and the second element is an array of 3 integers.

Now, we can treat a[0] and a[1] as normal arrays.

a[0] = {1, 2};
a[1] = {3, 4, 5};

We can also assign the values to a[0] and a[1] in following ways:

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

and

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

Since, a[0] and a[1] are normal arrays, we can access their elements as a[0][0], a[0][1], a[1][0], etc.

We can also initialize the values of each element of a jagged array separately.

a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;
a[1][2] = 5;

Let's take an example.

using System;

class Test
{
static void Main(string[] args)
{
int[][] a = new int[2][];

a[0] = new int[]{1, 2};
a[1] = new int[3];

a[1][0] = 3;
a[1][1] = 4;
a[1][2] = 5;

//printing
for(int i=0; i<a.Length; i++)
{
for(int j=0; j<a[i].Length; j++)
{
Console.Write($"{a[i][j]}\t"); } Console.Write("\n"); } } }  1 2 3 4 5 In this example, i is going from 0 to a.Length-1 (i < a.Length). So, it will vary from 0 to 1. For each value of i, j will also vary from 0 to length of a[i] - 1. a[i] is the one of the array element of the jagged array a. Now, varying j in a[i][j] will make us iterate on each element of array a[i]. ### C# Passing Jagged Array to Method We can also pass a jagged array to a method. Let's take an example. using System; class Test { static void Display(int[][] a) { for(int i=0; i<a.Length; i++) { for(int j=0; j<a[i].Length; j++) { Console.Write($"{a[i][j]}\t");
}
Console.Write("\n");
}
}
static void Main(string[] args)
{
int[][] a = new int[2][];

a[0] = new int[]{1, 2};
a[1] = new int[3];

a[1][0] = 3;
a[1][1] = 4;
a[1][2] = 5;

Display(a);
}
}

1  2
3  4  5

### C# Returning Jagged Array from Method

We can also return a jagged array from a method. Let's take an example.

using System;

class Test
{
static void Display(int[][] a)
{
for(int i=0; i<a.Length; i++)
{
for(int j=0; j<a[i].Length; j++)
{
Console.Write(\$"{a[i][j]}\t");
}
Console.Write("\n");
}
}

static int[][] JaggedReturn()
{
int[][] a = new int[2][];

a[0] = new int[]{1, 2};
a[1] = new int[3];

a[1][0] = 3;
a[1][1] = 4;
a[1][2] = 5;

return a;
}

static void Main(string[] args)
{
int[][] a = JaggedReturn();
Display(a);
}
}

1  2
3  4  5