Wednesday, September 08, 2010

Different arrays in C#, Jagged vs. Multi dimensional

Jagged array :
bool[][] myBools = new bool[2][];


Multi dimentional array : 
double[,] myDoubles = new double[2, 2];


One of the differences between jagged arrays and a multi-dimension array is that a multi-dimension array will allocate memory for every element of each dimension, whereas a jagged array will only allocate memory for the size of each array in each dimension that you define. Most of the time, you'll be using multi-dimension arrays, if you need multiple dimensions, and will only use jagged arrays in very special circumstances when you are able to save significant memory by explicitly specifying the sizes of the arrays in each dimension.


No comments: