System.Array
CopyTo
Clone
Array Methods
.NET

Difference between the System.Array.CopyTo and System.Array.Clone

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Array.CopyTo and Array.Clone both duplicate array contents, but they solve different problems. Clone creates a new array object with the same length and contents, while CopyTo copies elements into an existing destination array starting at a specified index.

What Clone Does

Clone allocates a new array of the same runtime type and length as the source array, then copies the elements into it.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        int[] original = { 10, 20, 30 };
8        int[] cloned = (int[])original.Clone();
9
10        cloned[0] = 99;
11
12        Console.WriteLine(string.Join(", ", original));
13        Console.WriteLine(string.Join(", ", cloned));
14    }
15}

Because the array object is new, changing cloned[0] does not change the first slot in original.

The important limitation is that Clone performs a shallow copy. If the array contains references to mutable objects, both arrays still point to the same objects.

What CopyTo Does

CopyTo requires a destination array that already exists. It copies the source elements into that destination starting at the index you provide.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        int[] source = { 1, 2, 3 };
8        int[] destination = { 9, 9, 9, 9, 9 };
9
10        source.CopyTo(destination, 1);
11
12        Console.WriteLine(string.Join(", ", destination));
13    }
14}

Output:

text
9, 1, 2, 3, 9

This is useful when you want to copy into part of a larger array or reuse an existing buffer.

The Most Important Difference

The simplest distinction is this:

  • 'Clone returns a new array'
  • 'CopyTo writes into an array you already have'

That difference affects memory allocation, API shape, and intent. Clone is good when you want a standalone duplicate. CopyTo is good when you control the destination layout.

Both Operations Are Shallow Copies

This matters a lot for reference types.

csharp
1using System;
2
3class Box
4{
5    public int Value;
6}
7
8class Program
9{
10    static void Main()
11    {
12        Box[] original = { new Box { Value = 5 } };
13        Box[] cloned = (Box[])original.Clone();
14
15        cloned[0].Value = 42;
16
17        Console.WriteLine(original[0].Value);
18        Console.WriteLine(cloned[0].Value);
19    }
20}

Both lines print 42 because the array was copied, not the Box instance inside it. The same shallow-copy rule applies to CopyTo.

Exceptions and Constraints

CopyTo will throw if the destination array is too small or if the destination index would cause the copy to run past the end. It also requires type compatibility between source and destination.

Clone avoids those destination-size issues because it always creates the new array for you, but it returns object, so callers typically cast the result back to the expected array type.

In many codebases, Array.Copy is also worth considering because it makes source and destination ranges more explicit.

When to Use Which

Use Clone when:

  • you want a new array of the same size
  • you want a quick shallow duplicate
  • you do not need to choose a destination offset

Use CopyTo when:

  • you already have a destination array
  • you want to copy into a specific position
  • you are managing buffers or partial array placement

The choice is mostly about ownership and destination control, not about deep versus shallow semantics, because both are shallow.

Common Pitfalls

The biggest pitfall is assuming Clone makes deep copies of the objects stored inside the array. It does not.

Another mistake is using CopyTo without allocating enough space in the destination array. That throws at runtime.

A third issue is forgetting that Clone returns object. In strongly typed code, you usually need an explicit cast.

Summary

  • 'Clone creates a new array with copied elements.'
  • 'CopyTo copies elements into an existing destination array.'
  • Both operations perform shallow copies.
  • Use Clone for a quick full duplicate.
  • Use CopyTo when you need placement control or want to reuse an existing buffer.

Course illustration
Course illustration

All Rights Reserved.