Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does typeof(Object[,][]).Name equal "Object[][,]"?

Tags:

Evaluating typeof(Object[,][]).Name gives Object[][,]

Similarly, typeof(Object[][,]).Name gives Object[,][]

Seems like the comma is moving for no reason.

What gives?

like image 438
RobSiklos Avatar asked Feb 14 '13 20:02

RobSiklos


2 Answers

Mixing ragged and rectangular arrays is a recipe for insanity. Your intuition about what a given type notation means is almost always wrong, or, at least, mine is.

Read my article on the subject and see if that helps you understand what is going on here:

http://blogs.msdn.com/b/ericlippert/archive/2009/08/17/arrays-of-arrays.aspx

Basically: C# and reflection notate mixed ragged/rectangular arrays in two different ways, each for their own reasons.

like image 83
Eric Lippert Avatar answered Sep 20 '22 04:09

Eric Lippert


Leaving aside the purpose for which you might want to declare such exotic array, it looks like the way the reflection produces the name of an array is recursive: first, it produces the name of array's element type, and then appends square brackets with the appropriate number of commas.

Object[,][] is a 2D array of 1D arrays; the element type of the 2D array is Object[], so the overall result is "Object[]"+"[,]".

Here is another illustration of the way the algorithm works:

typeof(Object[][,][,,][,,,][,,,,]).Name 

is

Object[,,,,][,,,][,,][,][] 

I don't see a reason behind it other than the efficiency of implementation: as long as it is consistent, it does not matter if the syntax matches C# or not. After all, C# is not the only language in the .NET universe.

like image 31
Sergey Kalinichenko Avatar answered Sep 21 '22 04:09

Sergey Kalinichenko