[quoted text, click to view] Edgile wrote:
> Hello,
>
> Bellow is a little except that creates a static array dynamically.
> However it creates
> a two dimensional one instead of a single dimensional. Why????
>
> public static void Main(string[] args)
> {
> int[] intArray = new int[]{2,3};
> Type type = intArray.GetType();
> Array array = Array.CreateInstance(type, 0);
> // array is a two dimensional array. Why?
> intArray = (int[]) array; // Throws InvalidCastException, as expected
> Console.WriteLine();
> }
It isn't a two dimensional array. You can test that
int[,] array2d = (int[,]) array; // InvalidCastException
and
array.Rank will be 1, not 2.
In fact it is a one dimensional array where the element type is
intArray.GetType() as you requested, that is each element is an array.
This array-of-arrays is sometimes called a jagged array.
If you want an array of the same type as the first array, use
intArray.GetType().MemberType