(C# Language) Arrays - Jagged and Multi-dimensional

Array can be used to store many variables of the same type. An array can be of one dimension, can be of multiple dimensions and can even be a jagged array, which is commonly called an array of arrays. The size of an array is decided at the time of creation, and cannot be altered later.
(Rev. 31-Oct-2024)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

Creating an Array

An array of one dimension can be created and initialized like this -


// declare one dimensional 
// array of 3 elements 
int[] arr1D = new int[3];

// declare and initialize 
// one dimensional array of 3 elements 
int[] myArr1D = new int[] { 4, 5, 0 };

Arrays of two dimensions can be created and initialized as like this. Notice the one comma inside square bracket.


// array of 2 rows and 3 columns 
int[,] arr2D = new int[2, 3];

// declare and initialize 
int[,] myArr2D = new int[,] { { 2, 1, 4 }, { 5, 6, 9 } };

Arrays of three dimensions can be created and initialized as like this. Notice the two comma inside square bracket.


// array of 2 x 3 x 5 
int[,,] arr3D = new int[2, 3, 5];

Video Explanation (see it happen!)

Please watch the following youtube video:

Arrays are of Array Type

In reality, arrays in C# are objects. They are not directly addressable blocks of memory as in C language. The blocks of an array are indirectly manipulated through functions and properties of a base class called System.Array.

System.Array is an abstract class.

Every array that you create has a Length property that gives the number of items held.

The Rank property gives the dimensions of a multi-dimensional array.

Other notable functions are Find, BinarySearch, Reverse and Sort.

Jagged Arrays

A jagged array is an array whose elements are arrays. We can call jagged arrays as arrays of arrays.

This code declares a one dimensional jagged array of three elements.


// jagged array of three 1D elements 
int[][] jarray = new int[3][];

A jagged array of multi-dimensional arrays is possible. This is an array whose elements are 2-dimensional elements.

This is a jagged array that contains three elements. Each element is a two dimensional element.


// jagged array of 2D array 
int[][,] myJaggedArray = new int[3][,];

// 0-th element is 1x3 array 
myJaggedArray[0] = new[,] { { 1, 4, 7 } };

// 1-th element is 2x2 array 
myJaggedArray[1] = new[,] { { 1, 7 }, { 3, 5 } };

// 2-th element is 4x1 array 
myJaggedArray[2] = new[,] { { 1 }, { 3 }, { 5 }, { 4 } };

// print all the elements 
foreach (var elem in myJaggedArray)
{
  foreach(var item in elem)
  {
    Console.Write($"{item} ");
  }

  Console.WriteLine();
}

  

The first element is a 1x3 array of 1 row and 3 columns. The second element myJaggedArray[1] is a 2x2 array and the last element is a 4x1 array of 4 rows and 1 column.

Printing an Array

An array implements an IEnumerable. It can, therefore, be iterated with a for-each loop.

The first for-each loop iterates over the elements of the jagged array.

But since each element of a jagged array is an array we can run a second nested for each loop over that element to print the elements.


// gives each element 
foreach (var elem in myJaggedArray)
{
  // each element is an array 
  // so another for-each gives access 
  // to the elements 
  foreach(var item in elem)
  {
    Console.Write($"{item} ");
  }

  Console.WriteLine();
}

  

Jagged Arrays vs Multi-dimensional Arrays

A jagged array is an array whose elements are arrays. We can call jagged arrays as arrays of arrays.

Each element of a jagged array can be different length. But each element of a multi-dimensional array must have the same number of elements.

A multi-dimensional array is a matrix of items.

All the elements of a multi-dimensional array are in contiguous blocks of memory. But the elements of a jagged array are themselves arrays that can be located in their own blocks of memory.


This Blog Post/Article "(C# Language) Arrays - Jagged and Multi-dimensional" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.