C# Queue Methods

About This Index
This page is the C# Queue Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. Examples use small fixed data such as Queue<int> and Queue<string>. A queue always keeps a fixed order (front to back), so the examples can print items directly and the output you see is the output you get.
Learn what a Queue is for on /c-sharp/queue. Use this page when you need a specific API such as Enqueue(), Dequeue(), or TryDequeue().
A queue is a line. Enqueue adds at the back, Dequeue removes from the front, and Peek reads the front. Calling Dequeue or Peek on an empty queue throws, so check Count or use TryDequeue / TryPeek.
How to Call a Queue Method
Every Queue method is an instance method, so you call it on your queue variable:
Queue<string> line = new Queue<string>();
line.Enqueue("Asha"); // add at the back
line.Enqueue("Ben");
string next = line.Peek(); // look at the front: "Asha"
string served = line.Dequeue(); // remove from the front: "Asha"
bool has = line.Contains("Ben"); // search the whole queue
string[] copy = line.ToArray(); // copy front to back
int waiting = line.Count; // 1 C# Queue Methods Table
All method guides in this C# tutorial series (12 tutorials), grouped by purpose.
Add / remove
4 methodsPut items at the back, take them from the front, or empty the whole queue. TryDequeue does the removal without throwing on an empty queue.
| Method | Description | Tutorial |
|---|---|---|
Enqueue() | Add an item at the back of the queue, so it waits behind everything already there. | Try it |
Dequeue() | Remove and return the item at the front of the queue; throws if the queue is empty. | Try it |
TryDequeue() | Try to remove the front item; returns true or false instead of throwing on an empty queue. | Try it |
Clear() | Remove every item from the queue in one call; Count becomes 0. | Try it |
Look
3 methodsSee the front item without removing it, or ask whether an item is anywhere in the queue.
| Method | Description | Tutorial |
|---|---|---|
Peek() | Read the item at the front of the queue without removing it; throws if the queue is empty. | Try it |
TryPeek() | Try to read the front item without removing it; returns false instead of throwing on an empty queue. | Try it |
Contains() | Check whether an item is anywhere in the queue; it scans from the front, so it is O(n). | Try it |
Copy / convert
2 methodsCopy the items, front to back, into a new array or into an array you already have.
Loop
1 methodWalk through the queue from front to back without removing anything.
| Method | Description | Tutorial |
|---|---|---|
GetEnumerator() | Get an enumerator that walks the queue from front to back; foreach uses it for you. | Try it |
Capacity
2 methodsReserve space before a big load, or give back unused space after a big clean-up.
| Method | Description | Tutorial |
|---|---|---|
EnsureCapacity() | Pre-size the queue so it can hold at least a number of items without growing again. | Try it |
TrimExcess() | Give back unused space after a queue shrinks; the items and their order stay the same. | Try it |
Frequently Asked Questions
Start with Enqueue()
Join the line: add your first items at the back of a queue and see how the order is kept.
12 people found this page helpful
