CopyPastehas never been so tasty!

Jagged Arrays in C#

by anonymous

  • 0
  • 0
  • 0

Jagged Arrays:


Jagged array is an array that has arrays as element. You can say that it is an array that has the collection of arrays as element. In other languages we call this two dimensional arrays. You can store any type of data or reference in jagged arrays. The advantage of jagged array is its indexing is fast but the disadvantage of jagged array is its allocation is slow therefore it is little bit complex.


The syntax of jagged array is


DataType[][] VariableName;


After declaring you have to initialize the jagged array with new operator. The syntax is

int[][] jaggedArray = new int[size][];

Here we have to give the size of array for example we give the size 5 than we can store 5 one dimensional arrays in it. And we have to initialize them. Here the figure which shows jagged arrays.

In this we declare a jagged array of size five and we store five arrays in it.


To understand let us consider this example, in this we declare a jagged array and initialize the array and it has 3 elements. And it is an array whose elements are arrays but each element has different size. And then by using loop we print these arrays.


To demonstrate make a console application and write the following code.





staticvoid Main(string[] args)

        {// Declare local jagged array with 3 rows.

int[][] jaggedArray = newint[3][];

jaggedArray[0] = newint[4]  { 1, 2, 3, 4 };

jaggedArray[1] = newint[3] {3,2,1};

jaggedArray[2] = newint[2] { 5,6};

for (int i = 0; i <jaggedArray.Length; i++)


int[] innerArray = jaggedArray[i];

for (int a = 0; a <innerArray.Length; a++)


Console.Write(innerArray[a] + " ");









Add A Comment: