On May 21, 5:03 am, james <james.w...@gmail.comwrote:
Hi Guys,
Would anyone mind telling me the difference between Array.Clone and
Array.CopyTo? I was asked this during an interview and got partial
credit for saying they were basically the same because they create
shallow copies. I ran ILDasm on Array and saw that they do the copy
using different IL calls. Does anyone know why one would be preferred
over the other?
Thanks,
James
Hi,
Array.Clone creates a shallow copy of the array, whilst Array.CopyTo
copies all the elements of the array to the specified array
(preallocated).
With CopyTo you can copy the elements to the destination array
starting at a specified destination array index, thus add elements to
a given array.
Besides, as Nich mentioned, Clone does the allocation it self and
returns an exact length array.
Cheers,
Moty