Reputation: 3521
How can I copy an Multidimensional Array of bool into other. The one dimension array use the length but I am not sure that this is right for multidimensional array.
bool[][] OldArray
bool[][] NewArray = new bool[][];
Array.Copy(NewArray,OldArray,NewArray.length)
Upvotes: 1
Views: 859
Reputation: 45155
That's not a multidimensional array, it's a jagged array (array of arrays).
bool[,] multiDimArray;
is multidimensional
bool[][] jaggedArray;
is jagged. The difference is that a multidimensional array is always rectangular while a jagged array doesn't have to be.
To copy a mutlidimensional array (the first case) you just multipy the lengths of each dimension together, because (from MSDN):
When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).
For a jagged, you'll need to loop through the first dimension and then Array.Copy
each inner array in turn.
Upvotes: 3
Reputation: 54897
Matt Burland is correct; that's a jagged array, and cannot be copied using Array.Copy
. However, since you're initializing it, you can use LINQ.
bool[][] oldArray;
bool[][] newArray = oldArray.Select(innerArray => innerArray.ToArray()).ToArray();
The inner copying internally uses Array.CopyTo
, so it's not as slow as an enumeration would be.
Upvotes: 1