Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Most people understand that there’s a difference between a “rectangular” and a “ragged” two-dimensional array.
int[,] rectangle = {
{10, 20},
{30, 40},
{50, 60} };
int[][] ragged = {
new[] {10},
new[] {20, 30},
new[] {40, 50, 60} };
Here we have a two-dimensional array with six elements, arranged in three rows of two elements each. And we have a one-dimensional array with three elements, where each element is itself a one-dimensional array with one, two or three elements.
That’s all very straightforward. Where things get brain-destroying is when you try to make a ragged array of two-dimensional arrays. Quick, don’t think, just answer.
int[,][] crazy;
What is the type of crazy?
Option one: It’s a one-dimensional array, each element is a two-dimensional array of ints.
Option two: It’s a two-dimensional array, each element is a one-dimensional array of ints.
OK, now that you have your snap answer, think about it carefully. Does your answer change?
I’m not going to tell you the answer just yet. Instead let’s explore the consequences of each possibility.
Consequence One
Surely the way you make any type into an array is to append [] to the type specification, right? But in option two, you stick the [,] into the middle.
Option two is weird. Option one is sensible.
But wait. If [,][] means "a 1-d of 2-d's", then the order you read it off the page opposes the order you say it -- it looks like two-d-thing-followed-by-one-d-thing, so why shouldn't it read "a 2-d of 1-d's"?
But then why does the "int" come first? By that logic it should come last.
Argh. Maybe option one isn't entirely sensible. Clearly something is not quite perfect with both options. Oh well. Let's move on.
Consequence Two
Now suppose that you wanted to obtain a value in that array, assuming that it had been initialized correctly to have plenty of elements everywhere we need them. How would you do it?
Suppose we’re in option one. It’s a one-d array. Therefore crazy[10] is a two-d array. Therefore crazy[10][1, 2] is an int.
Suppose we’re in option two. It’s is a two-d array. Therefore crazy[1,2] is a one-d array. Therefore crazy[1,2][10] is an int.
Option one is weird -- crazy is of type int[,][] but you dereference it as [10][1,2]! Whereas option two is sensible; the dereferencing syntax exactly matches the ordering of the type name syntax.
Consequence Three
Now suppose you want to initialize the “outer” array but are going to fill in the “ragged” interior arrays later. You’ll just keep them set to null at first. What’s the appropriate syntax to initialize the outer array?
Suppose we’re in option one. It’s a one-d array. Therefore it should be initialized as crazy = new int[,][20]; right?
Suppose we’re in option two. It’s a two-d array. Therefore it should be initialized as crazy = new int[][4,5]; right?
Option two is weird. We said int[,][] but initialized it as [][4,5]. Option one is sensible.
What C# actually does
It’s a mess. No matter which option we choose, something ends up not matching our intuition. Here’s what we actually chose in C#.
First off: option two is correct. We force you to live with the weirdness entailed by Consequence One; you do not actually make an element type into an array of that type by appending an array specifier. You make it into an array type by prepending the specifier to the list of existing array specifiers. Crazy but true.
This prevents you from having to live with any weirdness from Consequence Two; in this option, the dereferencing happens with the same lexical structure as the declaration.
What about Consequence Three? This one is the real mind-bender. Neither choice I offered you is correct; apparently I’m a sneaky guy. The correct way to initialize such an array in C# is:
crazy = new int[4,5][];
This is very surprising to people!
The design principle here is that users expect the lexical structure of the brackets and commas to be consistent across declaration, initialization and dereferencing. Option two is the best way to ensure that if declaration has the shape [,][] then the initialization also has that shape, and so does the dereferencing.
That all said, multidimensional ragged arrays are almost certainly a bad code smell. Hopefully you will never, ever have to use your new knowledge of these rules in a production environment.
Life is much better if you can instead use generic collections. It is completely clear what List<int[,]> means – that’s a list of two-dimensional arrays. Whereas List<int>[,] means a two-d array of lists of int.
Anonymous
August 17, 2009
The comment has been removedAnonymous
August 17, 2009
Enlightening, as always... thanks !Anonymous
August 17, 2009
It's interesting to note that this isn't a problem for pointers, because you cannot have a pointer to an array. So all the weird cases there are disabled anyway: int*[] a; // array of pointers int[]* a; // error int[][]; // error int[]; // error Anyway, the array declarator problem is something that always bugged me as awfully inconsistent, but then you can only do so much with the legacy C/C++/Java syntax. It's interesting to see how other languages from the family deal with the problem. Java simply weasels out. It doesn't have multidimensional arrays, nor pointers, so the only thing you can stick the [] declarator on is the base type, or another array declaration. Obviously, when they're all 1-dimensional, it doesn't matter which side you read it from, left or right - it's all the same: int[][] a; D 1.0 has much more to deal with, since it has static arrays a la C (with size specifier), and also pointers to arrays etc. This can quickly become a mess, so to retain some consistency, they go for "option 2", and read type modifiers consistently from right to left. So: int[3][4]; // array of 4 arrays of 3 ints int[][3] a; // array of 3 pointers to dynamic arrays of pointers to int Which is of course inconsistent with usage, since the order is reversed: (((a[1]))[2]) = 123; To mitigate that somewhat, D 1.0 also gives the option of declaring arrays C-style, with brackets following variable. For those declarations, the order is left-to-right, and thus consistent with C/C++: int a[4][3]; // array of 4 arrays of 3 ints int (*a[3])[]; // array of 3 pointers to dynamic arrays of pointers to int This is somewhat consistent in that if you want the declaration to match usage, you have to go all the way, and match the placement of [] in the declaration with usage as well (i.e. after the variable). But then two subtly different ways to do the same thing are a recipe for bugs, and D language reference itself recommends always sticking to the first, prefix form. Personally, I've always found this to be the area where Pascal-derived languages shine - especially Modula-2. You can't beat the clarity of: VAR a : ARRAY[1..3] OF POINTER TO ARRAY[1..10, 1..10] OF INTEGER; Pascal/Delphi are somewhat less readable (but more concise) when it comes to pointers, but the general idea is the same: types should always be read left to right in a natural way. Of course, if you do this, you want the type to be on the right of variable name in a declaration, otherwise it just looks weird. I mean, say we had: [,][]int a; // huh? I believe D 2.0 explored that at some point, but then backed away to the same behaviour as D 1.0. I can understand why - I'll take the existing C# syntax any day, thank you very much... By the way, it seems that C++0x might get Pascal-style declaration syntax as well. In the minutes of July 2009 WG21 meeting (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2920.html), there is this interesting bit: "Crowl, Stroustrup, and Vandevoorde will work on a Linear Declaration Syntax that is an alternative to and generalization of Unified Function Syntax. Some examples: [] p1: *const int; // Pointer to const int [] p2: *int const; // Error [] p3: (char)->*int; // Function talking a char returning pointer to int [] p4: ()->int; // Pointer to function returning an int [] p5: ()->()->int; // Function that might return p4 [] p6: [2][3] * [4] int;// Array of 2 arrays of 3 pointers to arrays of 4 ints" Of course, for C++ this is really long overdue because of the complexity of its type system, and the fact that declaring something like a pointer to array is a fairly common thing. In contrast, I do not remember the last time I had to write a multidimensional array in C#...Anonymous
August 17, 2009
I have avoided multi-dimensional arrays for years now. They are weaker than "ragged" arrays in most ways. The most important to me is speed - getting or setting rectangle[1,2] is about twice as slow as ragged[1][2].Anonymous
August 17, 2009
The comment has been removedAnonymous
August 17, 2009
The other fun part is that System.Type follows Option 1. So typeof(System.Int32[,][]).Name =="System.Int32[][,]" Someone recently filed a bug on the XAML parser because of this (we write out the type name using System.Type.Name), I had some fun explaining why it was By Design.Anonymous
August 17, 2009
Avoid ragged arrays and multi-dimensional ones with 3 or more subscripts. This 'too smart for your own good' code has bitten me several times usually at the expense of multple days of debuger fun.Anonymous
August 17, 2009
OK, I completely understand why C# language implementers care about this, so props to you for posting it. But for anyone else, the answer is, "It doesn't matter; just don't do it!"Anonymous
August 17, 2009
The comment has been removedAnonymous
August 17, 2009
@DRBlaise, Getting an element from a ragged array takes two accesses, while getting an element from a multi-dimensional array only takes one access. So multi-dimensional arrays should usually be faster, not slower.Anonymous
August 17, 2009
One to file under "things I hope I never have to worry about"Anonymous
August 17, 2009
Devil is in the detail, huh? :-) It's all well and good for us who USE the language to say "just don't do it", but the problem is, those who DESIGN the language cannot afford to overlook ANY possible syntactically valid use of the language they design, no matter how silly and impractical it looks (and I am NOT saying that arrays of arrays are silly and impractical: all I am saying is that even if they were, they still must work, full stop). Which brings us to an interesting question: how do you unit-test a language? Is it possible to produce the sufficient amount of code equivalents of human-language tongue-twisters to make sure the language is complete? How do you catch the programming-language equivalents of the English phrase "we eat what we can and what we can't eat, we can," where you're not sure what they said: that "what they couldn't eat, they could," or that "what they couldn't eat, they canned?" Ok, we humans can intuitively figure out these things, but computers don't have any intuition yet...Anonymous
August 17, 2009
@commongenius, Unfortunately, it doesn't work that way. 0-based 1D arrays have special IL opcodes to instantiate and access elements, and those are efficiently JIT-compiled, with all those nice optimizations such as eliding unnecessary bound checking in a loop. For multidimensional arrays, element access is implemented via method calls - just check the IL. And those calls are slow. That kills perf on the spot. All benefit you get from locality is lost from method access overhead - a 2D array is roughly 1.5 times slower than a corresponding jagged array because of that.Anonymous
August 17, 2009
What's wrong with int[][][] sane; ?Anonymous
August 17, 2009
@Pavel Minaev You are correct in saying that there are no special IL opcodes for multi-dimensional arrays, but that's no reason for it to be slow. The JITter could produce special inline native code for method calls to the Array object that get/set multi-dimensional arrays. The problem (so far as I know, and I could easily be out of date) is that the current JIT makes no attempt to do this. Theoretically this should be faster than jagged array access.Anonymous
August 17, 2009
Talking about driving crazy,I love playing with arrays: int[,][][][,,] arr = new int[10,12][][][,,]; arr[3, 5] = new int[6][][,,]; arr[3, 5][1] = new int[3][,,]; arr[3, 5][1][0] = new int[3, 2, 4]; arr[3, 5][1][0][0, 1,0] = 2; System.Console.WriteLine(arr[3, 5][1][0][0, 1,0]); :) But on second though, thank god for generics.Anonymous
August 18, 2009
The comment has been removedAnonymous
August 18, 2009
The comment has been removedAnonymous
August 18, 2009
My C++ brain is horrified at the thought that someone would even consider using #define (rather than typedef) for type aliases!Anonymous
August 18, 2009
This would not pass our code reviews since arrays beyond 2 dimensions or jagged arrays should be used only in very special circumstances (e.g., representing 3d data or a series of frames for 2d data). Eric, An interesting exmple of non-uniform array dimensions in how SQL Server 2000 stored variable length rows in a single data block. Throw in inter-page data compression with SQL 2008 and it gets more interesting.Anonymous
August 18, 2009
@Greg, I am somewhat suprised at your "policy" in this matter. Of course there is always the design decision between an array and an indexable (or keyed) collection. When using collections, it is not uncommon to have significantly more than 3 dimensions, and for multiple levels to be sparse (ie not all keys are valid, or indices have different maxiumums based on context. If one is to allow those conditions, then why (especially if bounds checking is in effect) the difference for arrays???Anonymous
August 18, 2009
Perhaps something to consider is that if you're getting to the stage of defining variables like this that you aren't "separating your concerns". Either you want an array of two-dimensional arrays or it is a two-dimensional array of arrays. Whichever one is correct, define yourself a class that represents the second part of the description. Then define a class defining the whole type. You now have a class representing your complicated object, allowing you to use things like indexers and composite patterns. There's no confusion, and (wow!) you're actually taking advantage of using an object-oriented language.Anonymous
August 18, 2009
The comment has been removedAnonymous
August 18, 2009
The comment has been removedAnonymous
August 19, 2009
This nonsense is exactly why those of us who grew up on languages other than C cringe at Ritchie's array notation. Every other language of C's age uses something like "int A(1,2,3)" and treats multi-dimensional arrays as first class citizens. Even FORTRAN got it right!Anonymous
August 19, 2009
@Ross, I "grew up" on languages that well predate "C" [I started programming in 1972], and actually found "C"'s approach to arrays to be quite good.... Just goes to show that generalizations always have exceptions [even this one...circuit overload....]Anonymous
August 19, 2009
@Ross, it's not quite true. Treating a multidimensional array as array-of-arrays is perfectly reasonable, and in fact both Wirth's Pascal and Modula-2 did it that way. I.e. they let you write: var a : array [1..2, 1..3] of integer; but it was really just syntactic sugar for: var a: array [1..2] of array [1..3] of integer; and, correspondingly, array access: a[1, 2] := 123; was syntactic sugar for: a[1][2] := 123; C simply disposed with the sugar. The real problem isn't the notion of array-of-arrays; it's C's inverted type modifier order. BASIC and Ada had true multidimensional arrays, however.Anonymous
August 20, 2009
I only use 1 dimesnional arrays. Even the language I wrote only allows 1 dimensional arraysAnonymous
August 20, 2009
@Dylan, thqat may be fine for a specializxed language, but how would you handle time corellated dimensional mapping. Basically a 4 dimensional array that you can slice along any access (or intersection plane) to get a 3 dimensional array which you can slice...2 dimensional ...slice...1 dimensional..slive value...Anonymous
August 20, 2009
"brain-destroying" --- I Couldn't have picked a better word :)Anonymous
August 21, 2009
I second Alex O. - there is nothing special about this and no surprises. Option 2 seemed to me very logical both to define and to use - so at least from my point of view this was a good design decision. Of course I'm not saying it's a good practice to use this style of coding - IMHO generics are the way to go ;)Anonymous
August 21, 2009
The comment has been removedAnonymous
August 22, 2009
1-D //Array of 10 uint social_security[10]; uint area_code[10]; uint zip_code[10]; //Access item i social_security[i]; area_code[i]; zip_code[i]; deck[];Anonymous
August 24, 2009
@Pavel: I had some contact with the .NET performance team due to some speed issue, and it turnet out that 2D Array access was one of several problems accumulating there. Besides the workaround of using jagged arrays, I was told that .NET 4.0 Beta 2 likely will have some improvements in the area of 2D arrays.Anonymous
August 25, 2009
Shouldn't RAGGED be JAGGED array? Either term is acceptable. "Jagged" appears to be the more common term in use on the Internet; I was taught "ragged". -- EricAnonymous
August 25, 2009
@Markus Hooray! Maybe they'll add mutli-dimensional array serialization too??Anonymous
August 25, 2009
The comment has been removedAnonymous
August 31, 2009
The comment has been removedAnonymous
August 31, 2009
The comment has been removedAnonymous
August 31, 2009
For me personally, the "inconsistent" rules would have been preferable. Somehow those are always the rules I think of when thinking of a particular situation (e.g., creating a new array). Possibly because they are actually more consistent with a concept I find important - that "int[]" is an inseparable definition of the type "array of ints". It's a pity that this is not true. When I don't remember a rule exactly I try to guess what it might be. So, wrongly assuming "int[]" is inseparable I always guess that you create a new array of 25 of these by appending "[25]". It could well be that others guess that the order would be the same as in indexing and type "int[25][]", but to me this feels completely alien. Like Timwi said, it would be interesting to see if you decided the consistency was important among potential C# users by running a study / poll of some sort.Anonymous
September 01, 2009
Both the int[,][] and List<int>[,] seem very natural to me; I can't see or understand any conflict. Even List<int[,]>[,][] is kinda obvious. It's two dimensional array, each element of the array is one dimensional array of List<int[,]>. Each elemnt of the list is rectanguar array of integers.Anonymous
September 04, 2009
The comment has been removedAnonymous
September 04, 2009
...D= My C++ brain is not accustomed to this...I'll leave C# to C# people. Bye now.Anonymous
September 14, 2009
I'm with Timwi and Roman, I don't even find consequence 2 weird for option one. It's kinda like accessing members/methods from a class.Anonymous
September 24, 2009
I'm with Timwi, Roman and Dominique - I find that design decision too "special casy", so to speak. It can never change though, so we'll have to live with it.Anonymous
September 26, 2009
The comment has been removedAnonymous
September 29, 2009
The comment has been removedAnonymous
November 15, 2012
This is why C's way of doing things makes more sense :D