Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
array

Differences between Array.CopyTo() and Array.Clone() in C#

Muhammad Shmoon

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

An array is a contiguous memory allocation. It is used to store data collection of the same data types. Most of the time, we need to copy one array to another to perform computations without altering the original array. There are various ways to execute the copy operation, but in this Answer, we will compare Array.CopyTo() and Array.Clone() methods.

The Array.CopyTo( ) method

The CopyTo method of the array class copies all the data in the array to an existing array. It performs a shallow copyCopying references to the same object insted of creating new objects.. It is used to copy a one-dimensional array. It copies the complete content of the array to the new array starting from the index passed in the parameter.

Syntax

The syntax of the CopyTo method of the Array class is:

Array.CopyTo(destArr, index);
  • Array is the array we want to copy the contents.
  • destArr is the array of the same data type as Array where the content will be copied to.
  • index is the integer from where we want to start copying on destArr.

Example

The following is an example of copying array data to a new array using the CopyTo() method.

class Educative
{
static void Main()
{
var arr1 = new[] { "Lodhi", "Educative", "Faheem", "Welcomes","You" };
var arr2= new string[10];
arr2[0]="Ed Tech";
// cloning arr and storing it in new array
arr1.CopyTo(arr2,1);
// Printing array using loop
foreach (var element in arr2)
{
System.Console.WriteLine(element);
}
}
}
Example of usage of CopyTo method

Explanation

  • Line 5: We initialize an array arr1 of size 5.
  • Lines 6–7: We initialize an array arr2 with a memory allocation of 10 indexes and assign the first index with a string "Ed Tech."
  • Line 9: We call the copyto() method of arr1 copying its elements to arr2 starting with index 1.
  • Lines 11–14: We print the elements of the array arr2.

The Array.Clone( ) method

The Clone() method of the Array class copies all the data in the array and returns a new object. It needs to be typecast to the datatype of the original array. It performs a shallow copy.Copying references to the same object insted of creating new objects. It can also be used to make copies of multi-dimensional arrays. It copies the complete content of the array stored in the new array.

Syntax

The following is the syntax of the Clone() method in C#:

(datatype[])array.Clone() 
  • datatype[] is the data type of the original array. It needs to be typecastThe conversion of one datatype to another..
  • array is the array that we want to copy.

Note: If you want to learn more about Array.Clone() method, check this answer.

Example

An example of copying an array by using the Clone() method is the following:

class Educative
{
static void Main()
{
var arr = new[] { "Lodhi", "Educative", "Faheem", "Welcomes","You" };
// cloning arr and storing it in new array
var new_arr = (string[])arr.Clone();
// Printing array using loop
foreach (var element in new_arr)
{
System.Console.WriteLine(element);
}
}
}

Major Differences

Array.Copyto( )

Array.Clone( )

Copyto( ) copies the source array to desitination array.

Clone() copies the array and returns the new object.

It takes two parameters, that is, the destination array and index.

It takes a single parameter.

It starts copying from the given index of the new array to the end of the array.

It always copies all content of the array.

It is slightly slower because it is a wrapper of the copy method.

It is slightly faster.

It does not need typecasting.

It needs to typecast the result to the original array datatype.

RELATED TAGS

c#
array

CONTRIBUTOR

Muhammad Shmoon
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring