The limit is 32. I checked it with the code:
var b = Array.CreateInstance(typeof (int), new int[32]);
var a = Array.CreateInstance(typeof (int), new int[33]);
It applies for both 64 and 32-bit targeted applications.
Actually, I needn't have googled it. Straight from MSDN:
An element is a value in an Array. The length of an Array is the total number of elements it can contain. The rank of an Array is the number of dimensions in the Array. The lower bound of a dimension of an Array is the starting index of that dimension of the Array; a multidimensional Array can have different bounds for each dimension. An array can have a maximum of 32 dimensions.
About jagged arrays
However, I just noticed that the syntax you used in your edit (and also the syntax Peter used in the comments below) isn't how multi-dimensional arrays are defined in C#. Multi-dimensional arrays are defined as:
int[,,,] arr = new int[0,0,0];
And they are arrays of regular integers, that happen to have multiple dimensions. The syntax:
int[][][] = new int[0][][];
Defines something that in C# is called a jagged array. Jagged arrays are just regular, single-dimensional arrays that happen to have other arrays as elements. You can have multi-dimensional arrays of jagged arrays, or the other way around, as the MSDN article points out.
They are treated differently, however. Jagged arrays are just arrays of (effectiely) regular* objects, and as such, aren't regulated in the same way as multi-dimensional arrays. There is no theoretical limit to the number of "dimensions" a jagged array can have, and in fact the CLR happily let me run the following code:
Array arr = new int[0];
for (int i = 0; i < Int32.MaxValue - 1; i++) {
var newArr = Array.CreateInstance(arr.GetType(), 1);
newArr.SetValue(arr, 0);
arr = newArr;
}
Right up until my system became completely unresponsive and crashed. No exception was thrown.
Limiting the loop to 1000 iterations, you find that the type of the resulting array is:
System.Int32[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]...
In addition, although I can't find a source for this, the number of dimensions a jagged array can have probably isn't even limited by the single object memory limitation, because each array only has references to objects. For example, a jagged array of (eventually) Int32
with a nesting factor of, say, 1000 and 2 elements per array, would take up a negligible amount of memory (that is, on its own), but all the arrays combined would take up an exponential amount.
* With a little bit of syntax sugar. A more syntactically correct way of encoding jagged arrays would be: int[][] arr = new int[][0];
because int[]
is the element type.
It's implementation defined
The number of dimensions an array can have appears to be implementation-defined. The number doesn't appear anywhere in the CLI specification, and the code provided by Peter below runs fine on Ideone's mono-2.8, where it appears the limit is 255.