Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between .MakeArrayType() and .MakeArrayType(1)

Tags:

arrays

c#

.net

According to the documentation of vs: MakeArrayType() represents one dimensional array with a lower bound of zero. MakeArrayType(1) represents an array with a specified number of dimensions. For example if the UnderlyingSystemType is int the return type of MakeArrayType() is System.Int32[] and the return type of MakeArrayType(1) is System.Int32[*].
What is the difference between those types.

like image 488
Sagi Avatar asked Aug 14 '11 16:08

Sagi


1 Answers

There is a subtle difference between .MakeArrayType() and .MakeArrayType(1) as you've seen from the type that is returned (Int32[] versus Int32[*]). According to the documentation for .MakeArrayType():

Note: The common language runtime makes a distinction between vectors (that is, one-dimensional arrays that are always zero-based) and multidimensional arrays. A vector, which always has only one dimension, is not the same as a multidimensional array that happens to have only one dimension. This method overload can only be used to create vector types, and it is the only way to create a vector type. Use the MakeArrayType(Int32) method overload to create multidimensional array types. Source

So when you call .MakeArrayType() it returns a Vector (which is a special thing that always has one dimension). Calling .MakeArrayType(1) makes a multi-dimensional array (not a Vector) - it just happens that it only has a single dimension.

The difference between a Vector and an Array are pretty technical but basically Vectors get special treatment by the CLR so there are additional IL instructions that work with them and that can make them more efficient. For more information about the difference between Arrays and Vectors see: http://markettorrent.com/community/7968#Vectors vs. Arrays

like image 153
Stephen McDaniel Avatar answered Oct 16 '22 14:10

Stephen McDaniel