Clone() Vs CopyTo() Array
The Clone() method returns a new array object containing all the elements in the original array. This method creates a copy of an array as an object, therefore needs to be cast to the actual array type before it can be used to do very much. The clone is of the same Type as the original Array.
The CopyTo() method copies the elements into another existing array. It copies the elements of one array to another pre-existing array starting from a given index (usually 0).
Both perform a shallow copy . Shallow copying is creating a new object and then copying the non static fields of the current object to the new object. If the field is a value type, a bit by bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not, therefore the original object and its clone refer to the same object. A shallow copy of an object is a new object whose instance variables are identical to the old object.
- What are the Default Access Modifiers in C#?
- What is a sealed class in C#?
- What is operator overloading in c#?
- Difference Between Finalize and Dispose Method in C#?
- What is an Object Pool in .Net?
- What is Anonymous type in C#?
- Dynamic type variables Vs Object type variables in C#?
- What is the difference between Array and ArrayList in C#?
- Difference between throw and throw new Exception() in C# ?
- What is the use of finally block in C#?