C# Queue CopyTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Queue

Definition

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.

C#
Queue<int> queue = new Queue<int>();
queue.Enqueue(10);
queue.Enqueue(20);
queue.Enqueue(30);

int[] buffer = new int[5];
queue.CopyTo(buffer, 1);    // buffer: 0, 10, 20, 30, 0
CallWhat it does
queue.CopyTo(buffer, 0)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

ParameterDescription
arrayThe one-dimensional destination array. It must not be null and must be large enough.
arrayIndexThe 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 ValueDescription
voidNothing 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);
    }
}

How It Works

  1. Right-sized array. new string[line.Count] gives exactly three empty cells, so there is room for every item.
  2. Start at zero. CopyTo(names, 0) writes the front item into cell 0 and the others into the cells after it.
  3. Front first. The joined text shows the same order as the line, and names[0] is Asha.
  4. 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));
    }
}

How It Works

  1. A longer buffer. The array has six cells, all set to -1 so changes are easy to see.
  2. Offset of 2. The front item 10 lands in buffer[2], then 20 in buffer[3], and 30 in buffer[4].
  3. 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.
  4. 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]);
    }
}

How It Works

  1. 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.
  2. Bad start position. An arrayIndex of -1 is outside the array, so the second call throws the more specific ArgumentOutOfRangeException.
  3. Names only. The code prints the exception type name and not the message, since the message wording can vary between .NET versions.
  4. 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.

Next: Dequeue()

Remove and return the item at the front of the queue; throws if the queue is empty.

Dequeue() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful