In C#, Queue.CopyTo(array, arrayIndex) copies all items of a Queue<T> into an array that already exists. The first item (the front) goes to array[arrayIndex], and the rest follow in order. The queue itself is not changed.
If ToArray() hands you a new box, CopyTo lets you choose the box and where to start packing. This is handy when you reuse a big buffer or want to put the queue items in the middle of a longer array.
This is an instance member of Queue<T> (System.Collections.Generic). Call it on a queue variable, such as line.CopyTo(...). A Queue<T> is a class, so two variables can share one queue. It is first in, first out (FIFO): items leave in the order they arrived. It is not thread-safe; for several threads use ConcurrentQueue<T>.
Syntax
Pass the destination array and the position where the first item should go.
Copy from the start of the array. The array must have room for queue.Count items.
queue.CopyTo(buffer, 2)
Copy starting at index 2. Cells before index 2 and after the copied items keep their old values.
queue.CopyTo(small, 0)
If the array has too little room, an ArgumentException is thrown and nothing is copied.
queue.ToArray()
When you do not already have an array. See ToArray().
The room needed is arrayIndex + Count cells. A null array throws ArgumentNullException. A negative arrayIndex, or one past the end of the array, throws ArgumentOutOfRangeException. If the index is valid but there is not enough space after it, you get ArgumentException. The method copies references for reference types (a shallow copy) and is O(n).
Library declarations
C#
public void CopyTo(T[] array, int arrayIndex);
Parameters
Parameter
Description
array
The one-dimensional destination array. It must not be null and must be large enough.
arrayIndex
The zero-based index in array where the front item is copied. It must be 0 or more and not past the end of the array.
Return Value
Return Value
Description
void
Nothing is returned. On success the items are written into array and the queue is unchanged.
C# Examples
Example 1 — Copy a Queue Into a Same-Size Array
Make an array as big as the queue and copy from index 0. The array shows the queue from front to back.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<string> line = new Queue<string>();
line.Enqueue("Asha");
line.Enqueue("Ben");
line.Enqueue("Chitra");
string[] names = new string[line.Count];
line.CopyTo(names, 0);
Console.WriteLine(string.Join(", ", names));
Console.WriteLine(names[0]);
Console.WriteLine(line.Count);
}
}
Output
Asha, Ben, Chitra
Asha
3
How It Works
Right-sized array.new string[line.Count] gives exactly three empty cells, so there is room for every item.
Start at zero.CopyTo(names, 0) writes the front item into cell 0 and the others into the cells after it.
Front first. The joined text shows the same order as the line, and names[0] is Asha.
Queue stays full. Copying does not remove anything, so Count is 3.
Example 2 — Copy Into the Middle of a Bigger Array
Fill a longer array with -1, then copy the queue starting at index 2. The cells around it keep their values.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> queue = new Queue<int>();
queue.Enqueue(10);
queue.Enqueue(20);
queue.Enqueue(30);
int[] buffer = new int[] { -1, -1, -1, -1, -1, -1 };
queue.CopyTo(buffer, 2);
Console.WriteLine(string.Join(", ", buffer));
}
}
Output
-1, -1, 10, 20, 30, -1
How It Works
A longer buffer. The array has six cells, all set to -1 so changes are easy to see.
Offset of 2. The front item 10 lands in buffer[2], then 20 in buffer[3], and 30 in buffer[4].
Cells outside are kept. Indexes 0 and 1 are before the start, and index 5 is after the last copied item. All three still hold -1.
Room check. The copy needs 2 + 3 = 5 cells and the array has 6, so it fits with one cell to spare.
Example 3 — What Goes Wrong With a Small Array
Try to copy three items into an array of two, and then use a negative index. Print the exception type for each.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> queue = new Queue<int>(new int[] { 1, 2, 3 });
try {
int[] small = new int[2];
queue.CopyTo(small, 0);
} catch (ArgumentException ex) {
Console.WriteLine(ex.GetType().Name);
}
try {
int[] big = new int[5];
queue.CopyTo(big, -1);
} catch (ArgumentOutOfRangeException ex) {
Console.WriteLine(ex.GetType().Name);
}
int[] fine = new int[3];
queue.CopyTo(fine, 0);
Console.WriteLine(fine[2]);
}
}
Output
ArgumentException
ArgumentOutOfRangeException
3
How It Works
Not enough room. Three items cannot fit in a two-cell array, so the first call throws ArgumentException, and the array is left as it was.
Bad start position. An arrayIndex of -1 is outside the array, so the second call throws the more specific ArgumentOutOfRangeException.
Names only. The code prints the exception type name and not the message, since the message wording can vary between .NET versions.
Fixed copy. A three-cell array and index 0 work. The last cell, fine[2], holds the queue's last item, 3.
Frequently Asked Questions
It copies all items of the queue into an existing array, starting at the arrayIndex you give. The front item goes first. The queue is not changed.
ToArray creates and returns a new array. CopyTo writes into an array you already have, at a position you choose, which can avoid an extra allocation.
An ArgumentException is thrown. The array must have at least arrayIndex + Count cells.
It must be 0 or more and not greater than the array length, and there must be enough cells after it for all the items. A negative value or one past the end throws ArgumentOutOfRangeException.
No. For reference types, it copies the references, so the array and the queue point to the same objects. That is a shallow copy.