Why are jagged arrays in C# defined in the opposite way?

Viewed 295

Let's say I have a type called T. Now let's say I make an array of type T, it gives me T[]. In code, it gives:

var myArray = new T[10];

With a length of 10. So, we can see that this makes an array that contains 10 items of type T. This works if T is int, string, BinaryWriter or anything standard. But let's say T is an array type, like int[] or string[].

Then, if we want to define an array of 10 items of type T (int[]), it should give something like this:

var myArray = new int[][10];

By replacing T by int[] in the previous example. But this gives a syntax error, because the correct syntax in C# is:

var myArray = new int[10][];

Which, if we followed the logic of the first example, should give an array containing an undefined amount of arrays containing 10 integers.

The same applies for any dimension of jagged arrays:

var myArray = new int[][][10];

Is wrong because the syntactically correct way is:

var myArray = new int[10][][];

This isn't a personal preference or a code style debate, but simply logic: why is the syntax different when we define arrays of array types, than when we define arrays of anything else?

2 Answers
Related