In C#, CopyTo is an instance method: you call it on the source array. It copies every element into another array starting at a destination index you choose.
It is a convenient form of “copy all of me into that array over there.” For partial ranges, use static Array.Copy instead.
This is an instance member of System.Array. You call it on an array variable with a dot, such as arr.CopyTo(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax
Call CopyTo on the source array. Pass the destination and the starting index.
C#
src.CopyTo(dest, 0);
src.CopyTo(dest, 2);
Call
What it does
src.CopyTo(dest, 0)
Copies all of src into dest starting at index 0.
src.CopyTo(dest, 2)
Leaves dest[0] and dest[1] alone; places src from index 2 onward.
dest must have enough room: index + src.Length <= dest.Length.
Library declarations
C#
public void CopyTo(Array array, int index);
public void CopyTo(Array array, long index);
Parameters
Parameter
Description
array
Required. Destination array.
index
Required. Starting index in the destination.
Return Value
Return Value
Description
void
Updates the destination in place.
C# Examples
Example 1 — Copy Into the Start
Place all source values at the beginning of a larger destination.
C#
using System;
class Program {
static void Main() {
int[] src = { 10, 20, 30 };
int[] dest = new int[5];
src.CopyTo(dest, 0);
Console.WriteLine(string.Join(", ", dest));
}
}
Output
10, 20, 30, 0, 0
How It Works
Instance call. The source is src; you do not pass it again.
Index 0. Writing starts at the first destination slot.
Full source. All three values are copied.
Tail zeros. Unused destination slots keep their defaults.
Example 2 — Copy Starting at Index 2
Leave the first two destination slots unchanged.
C#
using System;
class Program {
static void Main() {
string[] src = { "X", "Y" };
string[] dest = { "a", "b", "c", "d" };
src.CopyTo(dest, 2);
Console.WriteLine(string.Join(", ", dest));
}
}
Output
a, b, X, Y
How It Works
Offset. Destination index 2 is where "X" lands.
Prefix kept.a and b were never overwritten.
Two writes. Source length 2 replaces c and d.
Bounds check. Index 2 + length 2 fits in dest length 4.
Example 3 — CopyTo Matches a Full Array.Copy
Show the same result as Array.Copy(src, dest, src.Length).
C#
using System;
class Program {
static void Main() {
int[] src = { 4, 5 };
int[] a = new int[2];
int[] b = new int[2];
src.CopyTo(a, 0);
Array.Copy(src, b, src.Length);
Console.WriteLine(string.Join(",", a) == string.Join(",", b));
Console.WriteLine(string.Join(",", a));
}
}
Output
True
4,5
How It Works
Same content. Both techniques fill a same-sized destination.
Style choice. CopyTo reads nicely when the source is the subject.
Partial copies. Prefer static Copy when source indexes matter.
Equality check. Joined strings match, so the copies agree.
Frequently Asked Questions
It copies every element of the current array into another array starting at the given destination index.
CopyTo is an instance method and always copies the whole source. Array.Copy can copy a slice with source and destination indexes.
CopyTo throws an exception. Ensure index + source.Length is within destination.Length.
No. Only the written range changes. Other destination slots keep their old values.
The destination is treated as a flat Array. Prefer matching ranks and types for clarity.