In C#, Queue.ToArray() creates a new array and copies every item of a Queue<T> into it. The order is front to back: element 0 is the item that would come out first with Dequeue().
Think of taking a photo of the line. The photo shows everyone in order, but the people stay in the line, and later changes to the line do not change the photo. This is useful when you need an index (array[2]), want to sort or print the items, or need to loop over the queue and change it at the same time.
This is an instance member of Queue<T> (System.Collections.Generic). Call it on a queue variable, such as line.ToArray(...). 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
Call ToArray with no arguments. It returns an array of the same item type.
C#
Queue<string> line = new Queue<string>();
line.Enqueue("Asha");
line.Enqueue("Ben");
line.Enqueue("Chitra");
string[] snapshot = line.ToArray(); // { "Asha", "Ben", "Chitra" }
int n = snapshot.Length; // 3
Call
What it does
line.ToArray()
New array with the items from front to back. The queue keeps all its items.
Copy into an array you already have. See CopyTo().
ToArray is O(n) in time and memory, because it allocates a new array and copies every item. For reference types, the array holds the same object references as the queue, so the objects themselves are shared (a shallow copy). The returned array is yours: sorting it or changing an element does not touch the queue. Because ToArray reads the queue only once, it is also a safe way to loop over the items while you enqueue or dequeue.
Library declarations
C#
public T[] ToArray();
Parameters
Parameter
Description
None
ToArray() takes no parameters.
Return Value
Return Value
Description
T[]
A new array that holds a copy of the queue's items, front first. If the queue is empty, the array has length 0.
C# Examples
Example 1 — Copy a Queue Into an Array
Convert a queue of names to an array, print it, and check that the queue is the same as before.
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[] snapshot = line.ToArray();
Console.WriteLine(snapshot.Length);
Console.WriteLine(string.Join(", ", snapshot));
Console.WriteLine(snapshot[1]);
Console.WriteLine(line.Count);
}
}
Output
3
Asha, Ben, Chitra
Ben
3
How It Works
Same length. The queue has three items, so the new array has Length 3.
Front is index 0. The array starts with Asha, the person who would be served first, and ends with the newest person, Chitra.
Indexing works now. A queue has no [ ] access, but snapshot[1] gives Ben from the array copy.
Queue not drained.ToArray only copies, so line.Count is still 3.
Example 2 — The Array Is a Separate Copy
Change the array, then change the queue. Neither change reaches the other side.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> queue = new Queue<int>(new int[] { 10, 20, 30 });
int[] copy = queue.ToArray();
copy[0] = 999;
Console.WriteLine(queue.Peek());
queue.Enqueue(40);
Console.WriteLine(copy.Length);
Console.WriteLine(queue.Count);
Console.WriteLine(string.Join(", ", copy));
}
}
Output
10
3
4
999, 20, 30
How It Works
Edit the copy. Setting copy[0] = 999 changes only the array. The queue's front item is still 10.
Edit the queue.Enqueue(40) makes the queue longer, but the array does not grow. Arrays have a fixed length.
Two different sizes. The array has 3 elements while the queue now has 4 items, so the two really are separate.
What the copy kept. The last line shows the array as it is after your edit, 999, 20, 30, and it has no trace of the later 40.
Example 3 — Read the Last Item and Handle an Empty Queue
A queue shows only its front, so use ToArray to get at the back. Also see what an empty queue gives.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<string> pages = new Queue<string>();
pages.Enqueue("intro");
pages.Enqueue("body");
pages.Enqueue("summary");
string[] items = pages.ToArray();
Console.WriteLine(items[items.Length - 1]);
Queue<string> none = new Queue<string>();
string[] empty = none.ToArray();
Console.WriteLine(empty.Length);
Console.WriteLine(empty == null);
}
}
Output
summary
0
False
How It Works
No back access. A queue offers Peek() for the front only. To see the newest item you need an index.
Last element.items[items.Length - 1] reads the final array cell, which is summary, the item enqueued last.
Empty is fine. Calling ToArray() on an empty queue does not throw. It returns an array with Length 0.
Never null. The array is a real (empty) object, so empty == null is False and you can loop over it safely.
Frequently Asked Questions
It creates a new array and copies every item of the queue into it, from the front to the back. The queue is not changed.
Front to back. Element 0 is the item that would be returned first by Dequeue, and the last element is the most recently enqueued item.
No. It only copies them. Count stays the same, and the queue keeps all its items.
An empty array with Length 0. It never returns null.
Use ToArray when you need an index, a sort, or a stable snapshot to loop over while you change the queue. For plain reading, foreach is simpler and does not allocate a new array.