In C#, Array.Copy copies elements from a source array into a destination array. You can copy the whole array or a slice by passing starting indexes and a length.
Both arrays must allow the conversion of element types (usually the same type). The destination must be large enough for the range you copy.
This is a static member of System.Array. You call it with the class name, such as Array.Copy(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax
Call the static Array.Copy method with source, destination, and count (or with index offsets).
Copies 3 elements from index 0 of src to index 0 of dest.
Array.Copy(src, 1, dest, 2, 2)
Copies 2 elements starting at src[1] into dest starting at index 2.
For a safer “all or nothing” copy when conversion might fail mid-way, see ConstrainedCopy.
Library declarations
C#
public static void Copy(Array sourceArray, Array destinationArray, int length);
public static void Copy(Array sourceArray, int sourceIndex,
Array destinationArray, int destinationIndex, int length);
Parameters
Parameter
Description
sourceArray
Required. The array to read from.
destinationArray
Required. The array to write into.
sourceIndex / destinationIndex
Optional (overload). Starting indexes in each array.
length
How many elements to copy.
Return Value
Return Value
Description
void
No return value. The destination array is updated in place.
C# Examples
Example 1 — Copy Whole Array Prefix
Copy three ints into a larger destination and print both arrays.
C#
using System;
class Program {
static void Main() {
int[] src = { 1, 2, 3 };
int[] dest = new int[5];
Array.Copy(src, dest, src.Length);
Console.WriteLine("src: " + string.Join(", ", src));
Console.WriteLine("dest: " + string.Join(", ", dest));
}
}
Output
src: 1, 2, 3
dest: 1, 2, 3, 0, 0
How It Works
Count matters.src.Length is 3, so three values move.
Dest keeps size. Dest still has five slots; the last two stay 0.
Src unchanged. Copy reads source; it does not clear it.
Start at zero. This overload begins at index 0 in both arrays.
Example 2 — Copy a Middle Slice
Use indexes to copy only part of the source into a chosen destination offset.
C#
using System;
class Program {
static void Main() {
string[] src = { "a", "b", "c", "d" };
string[] dest = { "-", "-", "-", "-" };
Array.Copy(src, 1, dest, 0, 2);
Console.WriteLine(string.Join(", ", dest));
}
}
Output
b, c, -, -
How It Works
Source index 1. Copy starts at "b".
Length 2. Only "b" and "c" move.
Dest index 0. They land at the front of dest.
Rest untouched. The last two dashes remain.
Example 3 — Copy Into Equal-Length Target
Make a full duplicate when dest has the same Length.
C#
using System;
class Program {
static void Main() {
int[] src = { 9, 8, 7 };
int[] dest = new int[3];
Array.Copy(src, dest, 3);
dest[0] = 100;
Console.WriteLine("src[0] = " + src[0]);
Console.WriteLine("dest[0] = " + dest[0]);
}
}
Output
src[0] = 9
dest[0] = 100
How It Works
Independent storage. After Copy, dest is a separate array.
Mutate dest. Changing dest does not alter src.
Full length. Copying 3 into length 3 fills every slot.
Vs Clone. Copy needs a destination you already created; Clone allocates one.
Frequently Asked Questions
It copies a range of elements from a source array into a destination array.
No. The destination must be large enough for the indexes plus length you request.
Clone creates a new array and returns object. Copy writes into an existing destination.
ConstrainedCopy rolls back the destination if copying fails mid-way. Copy may leave a partial update.
Only when the runtime can convert each element. Same-type copies are the common case.