Both CopyTo() and Clone() make shallow copy (data is copied) and used for Single Dimension arrays.
Clone() method makes a clone of the original array. It returns an exact length array.
CopyTo() copies the elements from the original array to the destination array starting at the specified destination array index. Note that, this adds elements to an already existing array.
In VB.NET, variables has to be declared earlier, before using Clone or CopyTo methods to store the values. The difference arise on mentioning the size of the array.
While declaring the varaiable that is used for CopyTo method, it should have the size equal to or more than that of the original array.
While declaring the variable that is used for Clone method we need not mention the array size. Eventhough the array size is small, it won't show any error, rather the array size gets altered automatically on cloning. The cloned array size get created equal to the size of the source array.
If value type is used for CopyTo/Clone any change in the values made on them will not get reflected on the original whereas on reference type the change gets reflected.
Below is the code which would throw some light on your understanding.
Asked In: Many Interviews |