Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
System.ArrayCopyTo() vs System.ArrayClone() in C#
The CopyTo() and Clone() methods in C# are used to copy array elements, but they work differently. The CopyTo() method copies elements from one array to an existing array at a specified index, while Clone() creates a new array that is a shallow copy of the original array.
Understanding the difference between these methods is crucial for proper array manipulation and memory management in C# applications.
Syntax
Following is the syntax for the CopyTo() method −
sourceArray.CopyTo(destinationArray, startIndex);
Following is the syntax for the Clone() method −
object clonedArray = sourceArray.Clone();
Using Array.CopyTo() Method
The CopyTo() method copies all elements of the current array to an existing destination array starting at a specified index −
using System;
class Program {
static void Main() {
int[] arrSource = new int[4];
arrSource[0] = 5;
arrSource[1] = 9;
arrSource[2] = 1;
arrSource[3] = 3;
int[] arrTarget = new int[6];
// CopyTo() method - copies to existing array
arrSource.CopyTo(arrTarget, 1);
Console.WriteLine("Source Array: " + string.Join(", ", arrSource));
Console.WriteLine("Target Array after CopyTo: " + string.Join(", ", arrTarget));
}
}
The output of the above code is −
Source Array: 5, 9, 1, 3 Target Array after CopyTo: 0, 5, 9, 1, 3, 0
Using Array.Clone() Method
The Clone() method creates a shallow copy of the array and returns it as an object that must be cast to the appropriate type −
using System;
class Program {
static void Main() {
string[] arr = { "one", "two", "three", "four", "five" };
string[] arrCloned = arr.Clone() as string[];
Console.WriteLine("Original Array: " + string.Join(", ", arr));
Console.WriteLine("Cloned Array: " + string.Join(", ", arrCloned));
// Modifying original array
arr[0] = "modified";
Console.WriteLine("After modifying original:");
Console.WriteLine("Original Array: " + string.Join(", ", arr));
Console.WriteLine("Cloned Array: " + string.Join(", ", arrCloned));
}
}
The output of the above code is −
Original Array: one, two, three, four, five Cloned Array: one, two, three, four, five After modifying original: Original Array: modified, two, three, four, five Cloned Array: one, two, three, four, five
Comparison of CopyTo() and Clone()
| Feature | CopyTo() | Clone() |
|---|---|---|
| Target Array | Requires an existing array | Creates a new array |
| Return Type | void | object (needs casting) |
| Memory Usage | More efficient (no new array) | Less efficient (creates new array) |
| Flexibility | Can specify start index | Always copies entire array |
| Copy Type | Element-by-element copy | Shallow copy |
Practical Example Comparing Both Methods
using System;
class Program {
static void Main() {
int[] original = { 10, 20, 30, 40, 50 };
// Using CopyTo - copying to existing array
int[] copyToTarget = new int[7];
original.CopyTo(copyToTarget, 2);
// Using Clone - creating new array
int[] cloned = (int[])original.Clone();
Console.WriteLine("Original: " + string.Join(", ", original));
Console.WriteLine("CopyTo Result: " + string.Join(", ", copyToTarget));
Console.WriteLine("Clone Result: " + string.Join(", ", cloned));
// Memory allocation comparison
Console.WriteLine("CopyTo creates new array: False");
Console.WriteLine("Clone creates new array: True");
}
}
The output of the above code is −
Original: 10, 20, 30, 40, 50 CopyTo Result: 0, 0, 10, 20, 30, 40, 50 Clone Result: 10, 20, 30, 40, 50 CopyTo creates new array: False Clone creates new array: True
Conclusion
Use CopyTo() when you need to copy array elements to a specific position in an existing array for better memory efficiency. Use Clone() when you need a complete independent copy of the entire array. Both methods perform shallow copies for reference types.
