What is a jagged array?

ACP picture ACP · Apr 5, 2010 · Viewed 35.1k times · Source

What is a jagged array (in c#)? Any examples and when should one use it....

Answer

Anthony Pegram picture Anthony Pegram · Apr 5, 2010

A jagged array is an array of arrays.

string[][] arrays = new string[5][];

That's a collection of five different string arrays, each could be a different length (they could also be the same length, but the point is there is no guarantee that they are).

arrays[0] = new string[5];
arrays[1] = new string[100];
...

This is different from a 2D array where it is rectangular, meaning each row has the same number of columns.

string[,] array = new string[3,5];