C# Reference ยท Collections
The Queue<T> class
Reference for C# Queue<T>: Enqueue, Dequeue, Peek, TryDequeue and TryPeek, behavior on an empty queue, enumeration order, capacity and typical uses.
Queue<T> is a first-in, first-out (FIFO) collection. You add items at the back with Enqueue and take them from the front with Dequeue, so items leave in the same order they arrived. It is the natural structure for work lists, breadth-first search, buffering and anything that must be processed in arrival order.
Internally it is a circular array, so both Enqueue and Dequeue are O(1) amortized. You cannot access items by index or remove from the middle.
var jobs = new Queue<string>(); jobs.Enqueue("first"); jobs.Enqueue("second"); Console.WriteLine(jobs.Dequeue()); // first
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public class Queue<T> : IEnumerable<T>, IReadOnlyCollection<T>, ICollection { public Queue(); public Queue(int capacity); public Queue(IEnumerable<T> collection); public int Count { get; } public void Enqueue(T item); public T Dequeue(); public T Peek(); public bool TryDequeue(out T result); public bool TryPeek(out T result); public bool Contains(T item); public T[] ToArray(); public void Clear(); public int EnsureCapacity(int capacity); public void TrimExcess(); }
| Name | Description |
|---|---|
item |
The element to add at the back. |
capacity |
Initial or minimum internal array size; the queue still starts empty. |
result (out) |
Receives the front item, or default(T) when the queue is empty. |
Returns: Dequeue removes and returns the front item; Peek returns it without removing it. TryDequeue and TryPeek return true when an item was available. ToArray returns the items front to back.
Errors: Dequeue and Peek throw InvalidOperationException ("Queue empty.") when there are no items. Modifying the queue during foreach throws InvalidOperationException.
Usage notes
- FIFO order: the first item enqueued is the first dequeued. Use
Stack<T>for last-in, first-out. - Prefer
TryDequeuein loops that may run out of items, to avoid exceptions as flow control. - Enumeration is non-destructive and goes from front to back. There is no way to enumerate while consuming other than a
while (TryDequeue(...))loop. - No random access.
Containsis O(n), and there is noRemoveAt. If you need to remove arbitrary items, use aList<T>orLinkedList<T>. - Capacity grows by doubling. A queue that once held many items keeps its memory; call
TrimExcessto release it. - Not thread-safe. For producers and consumers on different threads use
ConcurrentQueue<T>,BlockingCollection<T>orSystem.Threading.Channels. - Priority instead of arrival order? Use
PriorityQueue<TElement,TPriority>. - Memory leaks: a queue that is filled faster than it is drained grows without bound; set a limit or use a bounded channel.