Thursday, July 12, 2007

What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?

  • The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array.
  • The CopyTo() method copies the elements into another existing array.

  • Both perform a shallow copy which means the contents (each array element) contains references to the same object as the elements in the original array.

  • CopyTo() handles type casting automatically but Clone() needs explicit casting.

No comments: