Search code examples
c#multidimensional-arrayjagged-arrays

C# jagged array type declaration in reverse


I just had one of these "What the..." moments. Is the following intended and is there some obscure reasoning behind the "non-natural" declaration of arrays in C#?

int[,][] i; // declares an 2D - array where each element is an int[] !
// you have to use it like this:
i = new int[2,3][];
i[1,2] = new int[0];

I would have expected the other way around. int[,][] declares an 1-dimensional array where each element is a two dimensional array.

Funny though, the type's Name is reversed:

Console.WriteLine(typeof(int[,][]).Name); // prints "Int32[][,]"

Can someone explain this? Is this intentionally? (Using .NET 4.5 under Windows.)


Solution

  • You can find a lengthy discussion in Eric Lippert's blog Arrays of arrays.

    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 [It’s a two-dimensional array, each element is a one-dimensional array of ints] 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.

    The word 'prepending' partly explains your output of the reversed type-name. A CLR type name is not necessarily the same as the C# declaration.

    But the more relevant quote is at the bottom:

    That all said, multidimensional ragged arrays are almost certainly a bad code smell.