---
title: "C# multidimensional array Tutorial"
url: https://weworkworldwide.com/tutorials/c-multidimensional-array-2/
description: "C# multidimensional array Tutorial: Before reading this section we're assuming you're already familiar with arrays. A multidimensional array is an array…"
date: 2026-11-19T17:00:28+00:00
source: https://weworkworldwide.com/llms.txt
---

# C# multidimensional array Tutorial

Before reading this section we’re assuming you’re already familiar with *arrays*.

A multidimensional array is an array that each of its elements represents another array. And those sub-arrays can themselves have another list of arrays as their elements and so on.

For example:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int[,] array = {
{1,2,3,4,5,6},
{7,8,9,0,1,2}
};
int [,]second = new int[10,10];
}
}
}
```

In this example the variable named `array` is a two dimensional array. As this particular example shows, the variable has two elements but each of these two elements is itself another array that has 6 integer elements.

The second array in this example is named `second` and it is another two dimensional array that has 10 elements and each of these elements is another array that can store 10 integer values.

Alright, now that we have a general view of how a multi-dimensional array looks like, let’s get into the syntax of such type of arrays:

``` line-numbers
Data-type [, ,]array-name = new Data-type [number-of-elements-in-first-array, number-of-elements-in-sub-array, number-of-elements-in-sub-sub-array ];
```

The `data-type` and `array-name` are the same in both single and multidimensional arrays.

But the way we use the pair of brackets `[]` on the left and right side of the assignment operator is different compared to a single dimensional array.

The pair of brackets that we put on the left side of the assignment operator defines how many dimension this array will have.

For example if we want to have a 3 dimensional array, we put the pair of bracket and add two commas in that bracket. Basically when we add one comma to the bracket it means the array is two-dimensional array. If we add two commas then that means the array is 3 dimensional array and so on.

Note: dimension means how deep the array is. For example three-dimensional array mean the array is 3 levels deep.

Now if we decide to just declare the array and later in the program assign values to it, then we need to put a pair of bracket on the right side of the assignment operator as well.

In this pair we should declare how many elements in each level the target array should contain?!

For example if we have two-dimensional array and we want the first level array to have 10 elements and each of those elements to have 12 sub-elements then the pair of brackets is defined like this: `[10 , 12]`.

Here the first level can have 10 elements and the second level can have 12 elements.

As you can see, we set the number of elements for each level and separated it from the other level via a comma `,`.

Note: On the left and right side of the assignment `=` operator, we should declare the same amount dimension (level). But in the left side brackets we don’t need to set the size for each dimension.

Example:

``` line-numbers
int [ , , ] integerArray = new int [220,10,30];
 double [ , ] doubleArray = new double [102,234];
 long [ , , ]longArray = new long [100,200,300];
```

In this example, the `integerArray` variable is a three-dimensional array:

1.  This array can take 220 elements and each of these elements is  
    itself another array of type integer.

2.  This sub-array can store 10 elements of type `int` but each of  
    these elements is itself another array.

3.  The sub-sub-array can store 30 elements of type `int` and  
    fortunately these elements are just simple integer values (No  
    sub-array).

The second variable is `doubleArray` and this variable is a two-dimensional array:

1.  The `doubleArray` is an array that can store 102 elements and  
    each of these elements is itself another array.

2.  This sub-array can store 234 elements of type `double` and each  
    of these elements can be just a simple double value.

The third variable is `longArray` and this variable is a three-dimensional array:

1.  The `longArray` is an array that can store 100 elements and each  
    of these elements is itself another array.

2.  This sub-array can store 200 elements and each of these elements  
    is itself another array.

3.  This sub-sub-array is an array that can store 300 elements of  
    type double.

Also when initializing a multi-dimensional array right where the declaration is happening, the number of elements for all sub-arrays should be the same.

For example if the first sub-array element has 10 values of type double, the second, the third and other elements of that array also should be an array of 10 values of type double.

Example:

``` line-numbers
int [ , ] array = {
 {1,2,3,4,5,6},
 {7,8,9,0,1,2}
 };
```

In this example, both the first and the second element of the array are sub-arrays that can store 6 integer values.

In order to access elements in a multi-dimensional array, we use a pair of brackets just like the way we used it for single-array.

For example let’s say we have a two-dimensional array:

``` line-numbers
new double doubleArray[12,24];
```

Now let’s say we want to access the third element of the first array list in the `doubleArray`:

``` line-numbers
doubleArray[0, 2];
```

Or let’s say we want to access the 20^(th) element of the second array list in the `doubleArray`:

``` line-numbers
doubleArray[1,19];
```

## So inside the pair of brackets, the first number that we set gives us the access to the elements of the first array and the second one gives us the access to the elements of the sub-array and so on.

## Looping through elements in a multi-dimensional array:

To loop through a multidimensional array, we need a for-loop for each dimension of the array.

For example if we have a 2 dimensional array, there should be 2 loops (the second one goes inside the first loop) in order to loop through all the elements within the array and the sub-arrays (the elements of the first array).

Example:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int [,] age= {
{ 80,90,93,50,99,76,100,88,99,30},
{ 50,96,97,50,91,74,59,83,93,38},
{ 84,91,97,54,91,75,10,85,96,36},
{ 60,81,90,54,56,89,78,98,78,98},
{ 95,45,98,62,85,78,98,100,64,80}
};
int firsSize = age.Length;
int secondSize = age.GetLength(1);
for (int i = 0; i < firsSize; i++)
{
for (int b = 0; b < secondSize; b++)
{
Console.Write(age[i,b] + ", ");
}
}
}
}
}
```

Output:

80, 90, 93, 50, 99, 76, 100, 88, 99, 30, 50, 96, 97, 50, 91, 74, 59, 83, 93, 38, 84, 91, 97, 54, 91, 75, 10, 85, 96, 36, 60, 81, 90, 54, 56, 89, 78, 98, 78, 98, 95, 45, 98, 62, 85, 78, 98, 100, 64, 80,

As you can see the variable set in the first loop `i` is set as the index number of the first array and the second variable `b` that is set in the second loop will be considered as the index number of the sub-array.

Note: we use the `GetLength()` property to get the length of the target array. The method takes one argument and that is the level of the target array. For example if we want to know the length of the first array, we put the value 0 in the parentheses. If we want to get the length of the first sub-array, we put the value 1 as the argument of the method and so on…

Let’s refactor the example above, but this time we use the `foreach` loop:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int [,]age = {
{ 80,90,93,50,99,76,100,88,99,30},
{ 50,96,97,50,91,74,59,83,93,38},
{ 84,91,97,54,91,75,10,85,96,36},
{ 60,81,90,54,56,89,78,98,78,98},
{ 95,45,98,62,85,78,98,100,64,80}
};
```

foreach (int element in age)

``` line-numbers
{
Console.Write(element + ", ");
}
}
}
}
```

Output:

80, 90, 93, 50, 99, 76, 100, 88, 99, 30, 50, 96, 97, 50, 91, 74, 59, 83, 93, 38, 84, 91, 97, 54, 91, 75, 10, 85, 96, 36, 60, 81, 90, 54, 56, 89, 78, 98, 78, 98, 95, 45, 98, 62, 85, 78, 98, 100, 64, 80,

When we use the `foreach` loop, it will go through all the elements of the array and sub-arrays and it will return the final non-array element.

## Basically it will flatten the array and returns the final values.
