In C#, List.CopyTo(array) copies the items of the list into an array that you already created. It does not make a new array. The first list item goes into slot 0 of the array.
The longer overloads let you choose where in the array to start writing, and copy only part of the list. If the array is too small, the call throws and nothing is copied. If you do not have an array yet, ToArray() creates one for you.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.CopyTo(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Create an array that is big enough first, then pass it in. Optional arguments set where to start.
Copies all items to the array, starting at array index 0.
numbers.CopyTo(array, 2)
Starts writing at array index 2.
numbers.CopyTo(1, array, 0, 2)
Copies 2 items, starting at list index 1, into the array from index 0.
If the array does not have room for all copied items, you get ArgumentException. A null array throws ArgumentNullException. Slots that are not written keep their old values. A new int[] is full of 0. Compare this with Array.Copy, a static method that copies between two arrays.
Library declarations
C#
public void CopyTo(T[] array);
public void CopyTo(T[] array, int arrayIndex);
public void CopyTo(int index, T[] array, int arrayIndex, int count);
Parameters
Parameter
Description
array
Required. The destination array. It must be big enough for the copied items.
arrayIndex
Optional overload. The array index where the first item is written.
index
Optional overload. The list index where copying starts.
count
Optional overload. How many items to copy.
Return Value
Return Value
Description
void
Nothing is returned. The array is filled in place. The list is unchanged.
C# Examples
Example 1 — Copy a List into an Array
Create an array of the right size, then copy all list items into it.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> { "Asha", "Ben", "Chen" };
string[] array = new string[names.Count];
names.CopyTo(array);
Console.WriteLine(string.Join(", ", array));
Console.WriteLine(array.Length);
}
}
Output
Asha, Ben, Chen
3
How It Works
Array first.new string[names.Count] makes an array with exactly as many slots as the list has items.
Fill from slot 0. With only the array argument, CopyTo writes the first item into slot 0, the next into slot 1, and so on.
Same order. The array holds the names in the same order as the list.
No new array. The method returns nothing. Your own array variable was filled.
Example 2 — Copy into the Middle of an Array
Use the array index to leave the first slot free, in a buffer that is larger than the list.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 10, 20, 30 };
int[] buffer = new int[6];
numbers.CopyTo(buffer, 2);
Console.WriteLine(string.Join(", ", buffer));
}
}
Output
0, 0, 10, 20, 30, 0
How It Works
Bigger array. The buffer has 6 slots, and a new int array starts with 0 in every slot.
Start at 2.CopyTo(buffer, 2) writes 10 into slot 2, 20 into slot 3, and 30 into slot 4.
Other slots untouched. Slots 0, 1, and 5 are not written, so they keep their 0.
Room check. Three items from index 2 need slots 2 to 4, and the array has slots up to 5, so there is enough room.
Example 3 — Copy Only a Part, and a Too-Small Array
Copy two items from the middle of the list. Then try an array that is too small and catch the error.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
int[] part = new int[2];
numbers.CopyTo(1, part, 0, 2);
Console.WriteLine(string.Join(", ", part));
int[] tooSmall = new int[3];
try {
numbers.CopyTo(tooSmall);
} catch (ArgumentException) {
Console.WriteLine("array is too small");
}
Console.WriteLine(tooSmall[0]);
}
}
Output
20, 30
array is too small
0
How It Works
Four arguments.CopyTo(1, part, 0, 2) reads from list index 1, writes into the array from index 0, and copies 2 items.
Only the middle. The items at list indexes 1 and 2 are 20 and 30, so those two fill the array.
Too small. The full list has 5 items, but tooSmall has 3 slots. The call throws ArgumentException, which the catch block reports.
Nothing half copied. The failed call writes no items, so tooSmall[0] is still 0.
Frequently Asked Questions
It copies list items into an existing array that you pass in, starting at array index 0 or at the index you choose.
ToArray creates and returns a new array. CopyTo fills an array you already own and lets you pick the position.
CopyTo throws ArgumentException and copies nothing. Make the array at least arrayIndex + the number of items long.
Yes. Use CopyTo(index, array, arrayIndex, count) to pick the list start and the number of items.
No. The list stays as it is. Only the destination array is written to.