In C#, Queue.Enqueue(item) puts one item at the back of a Queue<T>. A queue is first in, first out (FIFO): the item that has waited longest is served first, and a new item always waits behind the others.
Think of a line at a ticket counter. A new person joins at the end of the line; nobody can jump to the front. Enqueue is the "join the line" step. The matching "leave the line" step is Dequeue(), which takes the person at the front.
This is an instance member of Queue<T> (System.Collections.Generic). Call it on a queue variable, such as line.Enqueue(...). 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 Enqueue with one item. It returns nothing, so there is no result to check.
C#
Queue<string> line = new Queue<string>();
line.Enqueue("Asha"); // line: Asha
line.Enqueue("Ben"); // line: Asha, Ben (Ben is at the back)
int waiting = line.Count; // 2
Call
What it does
line.Enqueue("Asha")
Adds "Asha" at the back of the queue. Count goes up by 1.
line.Enqueue("Asha") (again)
Allowed. A queue keeps duplicates, so the same value can wait twice.
line.Enqueue(null)
Allowed for reference types such as string. The queue stores the null like any other item.
line.Dequeue()
The other side: take the item at the front. See Dequeue().
Enqueue is O(1) on average. When the internal array is full, the queue allocates a bigger one and copies the items over, which takes more time, but this happens rarely. If you know how many items are coming, EnsureCapacity() can avoid that growing. A Queue<T> has no indexer, so you cannot insert in the middle.
Library declarations
C#
public void Enqueue(T item);
Parameters
Parameter
Description
item
The item to add at the back. For a reference type such as string it may be null. Repeated values are fine.
Return Value
Return Value
Description
void
Nothing is returned. After the call, Count is 1 higher and the new item is the last one in line.
C# Examples
Example 1 — Three People Join a Line
Add three names to a queue, then check how many are waiting, who is at the front, and the order of the line.
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");
Console.WriteLine(line.Count);
Console.WriteLine(line.Peek());
Console.WriteLine(string.Join(", ", line));
}
}
Output
3
Asha
Asha, Ben, Chitra
How It Works
Empty at first.new Queue<string>() starts with no items. Each Enqueue call adds one name at the back.
Count grows. After three calls, Count is 3. Enqueue itself returns nothing, so Count is how you see the change.
Front stays put.Peek() shows "Asha", the first person who joined. Adding Ben and Chitra did not move her.
Order is kept.string.Join walks the queue from the front to the back, so the line prints in the same order the names were added.
Example 2 — Duplicates and null Are Allowed
Add the same tag twice and a null, then take the items out to see that all three were kept.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<string> tags = new Queue<string>();
tags.Enqueue("urgent");
tags.Enqueue("urgent");
tags.Enqueue(null);
Console.WriteLine(tags.Count);
Console.WriteLine(tags.Dequeue());
Console.WriteLine(tags.Dequeue());
Console.WriteLine(tags.Dequeue() == null);
Console.WriteLine(tags.Count);
}
}
Output
3
urgent
urgent
True
0
How It Works
No duplicate rule. Unlike a set, a queue never refuses an item. Both "urgent" values are stored, so Count is 3.
null is a normal item. For a reference type, Enqueue(null) stores a real slot that holds null. (If your project has nullable warnings turned on, declare the queue as Queue<string?>.)
Taken in join order. The first two Dequeue() calls return the two "urgent" items, then the third returns the null, which the comparison == null confirms.
Empty again. Each dequeue removed one slot, so the last Count is 0.
Example 3 — Add New Jobs While Others Are Being Served
A print queue starts with two files. While the first file prints, a late file joins the line. It must still wait for its turn.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<string> jobs = new Queue<string>();
jobs.Enqueue("report.pdf");
jobs.Enqueue("photo.png");
int printed = 0;
while (jobs.Count > 0) {
string job = jobs.Dequeue();
Console.WriteLine("Printing " + job);
printed++;
if (printed == 1) {
jobs.Enqueue("late.txt");
}
}
Console.WriteLine(printed);
}
}
Start with two. The queue holds report.pdf at the front and photo.png behind it.
Serve the front. Each loop turn removes the front job and prints it. The first turn prints report.pdf.
A late arrival. During that first turn, Enqueue("late.txt") puts the new job at the back, behind photo.png, which was already waiting.
Fair order. The loop keeps going while Count is above 0, so the jobs print in arrival order and the counter ends at 3.
Frequently Asked Questions
It adds one item to the back of a Queue<T>. The item waits behind every item that is already in the queue, and Count goes up by 1.
No. It returns void. Check the Count property if you want to see how many items are waiting.
Yes. A queue keeps duplicate values. null is allowed when the item type is a reference type such as string. For a value type such as int there is no null.
At the back. The first item you add stays at the front until you remove it with Dequeue, because a queue is first in, first out.
No. If several threads add and remove items at the same time, use ConcurrentQueue<T> from System.Collections.Concurrent, or protect the queue with a lock.