The problem arose as follows: create a method that extends an array of arbitrary dimension.

Unfortunately, the basic constructors of arrays have a syntax tied to the dimension. However, the Array class has the CreateInstance ( Type, Int32 [] ) method, which allows you to easily create the necessary array ... However, I did not find how to convert Array to a standard type, while maintaining the specified structure. The Copy () method in this situation refuses to work.

Help, if anyone knows, or explain, please, if I do not understand something of principle.

  • And the structure of the array can be in the studio? For example, array [1,2,3,4,5] and even theoretically do not transform, because there is no TN, what to convert, what actions to perform? - Garrus_En February
  • How did you try to use the Copy method? why do you think he refuses to work? - Grundy
  • @Grundy Array.Copy considers a two-dimensional array to be very long one-dimensional - and does not save strings (probably) - PashaPash

2 answers 2

Since all arrays in c # are inherited from Array, type casting should help you.

Array myArr = Array.CreateInstance(typeof(int), 2, 3, 4); int[,,] myvar = (int[,,])myArr; 
  • This was the first thing I checked out. And, alas, it does not work that way. Or rather, it works, but somehow it is completely wrong, trying to bring it to an array of arrays - wemon

Thank you all for the comments. The reason, as always, turned out to be nonsense: running from crutches, stuck in CreateInstance GetType () ... but not an element, but an array. The brain almost exploded from misunderstanding, and the browser from the tabs from msdn, but everything turned out to be much simpler (non-understanding handwriting)