# 2d Array in C#

## 2d Array in C# with Examples

In this article, I am going to discuss the 2d Array in C# with Examples. Please read our previous article before proceeding to this article where we discussed one-dimensional Arrays in C# with examples.  As part of this article, we are going to discuss the following pointers which are related to Two-Dimensional Array in C#.

1. What is Two-Dimensional Array in C#?
2. Understanding the rectangular and jagged array in C# with examples.
##### What is Two-Dimensional Array in C#?

The arrays which store the elements in the form of rows and columns are called Two-Dimensional Array in C#. The two-dimensional array you can also say multidimensional array are of two types in C#. They are as follows

1. Rectangular array: The array whose rows and columns are equal are called rectangular array
2. Jagged array: The array whose rows and columns are not equal are called jagged array
##### Rectangular 2d Array in C#:

Let us first understand the syntax of Two-Dimensional Array in C#. Please have a look at the following diagram. Let us see an example for a better understanding of the rectangular array in C#.

```namespace TwoDimensionalArayDemo
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[4, 5];
int a = 0;

//printing the values of 2d array using foreach loop
//It will print the default values as we are not assigning
//any values to the array
foreach (int i in arr)
{
Console.Write(i + " ");
}
Console.WriteLine("\n");

//assigning values to the array by using nested for loop
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
a += 5;
arr[i, j] = a;
}
}

//printing the values of array by using nested for loop
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ");
}
}
}
}
}```

OUTPUT: In the above example, we assigned the two-dimensional array element using nested for loop. It is also possible that we can assign the values to two-dimensional array in C# at the time of its declaration:

##### Assigning values to two-dimensional array in C# at the time of declaration:
```namespace TwoDimensionalArayDemo
{
class Program
{
static void Main(string[] args)
{
//Assigning the array elements at the time of declaration
int[,] arr = {{11,12,13,14},
{21,22,23,24},
{31,32,33,34}};

//printing values of array using for each loop
foreach (int i in arr)
{
Console.Write(i + " ");
}
Console.WriteLine("\n");

//printing the values of array using nested for loop
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ");
}
}

}
}
}```

OUTPUT: ##### Jagged Array in C#:

These are also two-dimensional array which will also store the data in the forms of rows and columns. But here in the jagged array, the column size will differ from row to row. That means if the first row contains 5 columns then the second row may contain 4 columns while the third row may contain 10 columns. So the point that you need to remember is if the column size varies from row to row then it is a jagged array. If the column size remains the same for all the rows then it is a rectangular two-dimensional array.

The jagged array in C# is also called the array of arrays. This is because in the case of the jagged array each row is a single dimensional array. So a combination of multiple single-dimensional arrays with different column sizes form a jagged array in C#.

Syntax:  <type> [][] <name> = new <type> [rows][];

Example:

```int [][] arr = new int[];
//Or
int [][] arr = {list of values};```

To declare a jagged array in C#, at the time of its declaration, you only need to specify the number of rows that you want in the array. for example

int [][] arr = new int[];

In the above array declaration, we are specifying that we want four rows in the array. Once you specify the number of rows that you want in the array, then you need to initialize each row with the number of columns by using a single dimensional array as shown below.

arr = new int; // we want five columns in the first row
arr = new int; // we want six columns in the first row
arr = new int; // we want four columns in the first row
arr = new int; // we want five columns in the first row

###### Example of the jagged array in C#:
```namespace TwoDimensionalArayDemo
{
class Program
{
static void Main(string[] args)
{
//Creating an jagged array with four rows
int[][] arr = new int[];

//Initializing each row with different column size
// Uisng one dimensional array
arr = new int;
arr = new int;
arr = new int;
arr = new int;

//printing the values of jagged array using nested for loop
//It will print the default values as we are assigning any
//values to the array
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
Console.Write(arr[i][j] + " ");
}
}
Console.WriteLine();

//assigning values to the jagged array by using nested for loop
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
arr[i][j] = j++;
}
}

//print values the values of jagged array by using foreach loop within for loop
for (int i = 0; i < arr.GetLength(0); i++)
{
foreach (int x in arr[i])
{
Console.Write(x + " ");
}
}
}
}
}```

When we run the application, it will give us the following output: In the above example, we are assigning the elements of the jagged array by using nested for loop. It is also possible to assign the values of the jagged array at the time of its declaration.

##### Assigning jagged array at the time of its declaration:
```namespace TwoDimensionalArayDemo
{
class Program
{
static void Main(string[] args)
{
// Assigning the values of the jagged array
// at the time of its declaration
int[][] arr = {
new int{11,12,13,14},
new int{21,22,23,24,25},
new int{31,32,33}
};

//printing the values of jagged array by using nested for loop
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
Console.Write(arr[i][j] + " ");
}
}
Console.WriteLine();

//print the values of jagged array by using foreach loop within for loop
for (int i = 0; i < arr.GetLength(0); i++)
{
foreach (int x in arr[i])
{
Console.Write(x + " ");
}
} 