8

I just saw this question and infact tried to answer it as well. But while answering I thought what could be the answer in case of C#? I am looking for some MSDN doc or any relevant source like in Java

What is the maximum dimension allowed for an array in C# like a[1][1][1][1]....[1]. I tried to search on SO but was not able to find.

The best I got up to was that "An array could theoretically have at most 2,147,483,647 elements, since it uses an int for indexing."

Also I am aware of the fact that Maximum Size that an Array can hold is

System.Int32.MaxValue

Kindly do let me know if this is a duplicate I will delete my question.

6
  • Also different question here, you are asking limit of elements in array, the question you reference asks the maximimum number of dimensions an array can have Commented Mar 28, 2014 at 13:09
  • @BobVale:- Yes I am asking the same in C#. Commented Mar 28, 2014 at 13:10
  • @Steve:- I dont think this is similar to the one which you linked. I am aware of the limit. I am asking about the dimension? Commented Mar 28, 2014 at 13:11
  • As a sidenote, why do you want an answer to this question? Are you trying to do something specific? You almost always want to avoid multi-dimensional arrays. You can also design a simple data structure with no limit on dimensions. Commented Mar 28, 2014 at 13:20
  • 1
    @GregRos:- Yes I fully agree and I am aware of that. As I mentioned in my question, I just saw that question in Java so out of curiosity I asked it in C# :) Commented Mar 28, 2014 at 13:23

1 Answer 1

21

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.

Sign up to request clarification or add additional context in comments.

7 Comments

My bad that I didnt go till the Remarks section! :(
If this is true, can somebody explain why I can create an array with 50 dimensions here: ideone.com/Bh59zd
What you say about jagged arrays is correct. As it turns out I can even create multidimensional arrays with more than 32 dimensions on ideone.com, e.g: ideone.com/JCo2bV it seems ideone runs on Mono and Mono has an upper limit of 255 on the number of dimensions when creating a multidimensional array. It would seem that the maximum number of dimensions is implementation specific, I could not find a maximum defined in the language spec.
+1 for the horror of [][][][][][]... and actually running it until it crashed.
@PetervanderHeijden that's is interesting. I couldn't find a maximum in the CLI spec either (not sure if that's the one you're referring to). I'll edit my answer to reflect that information.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.