Queues
A Queue<T> hands values back in the order they arrived, first in, first out. Enqueue adds to the back of the line, Dequeue takes from the front, and Peek looks without taking.
Journeyman · C# · Updated
After this lesson you can
- Use a Queue<T> for first-in, first-out work, with Enqueue, Dequeue, Peek and TryDequeue
Key takeaways
- A
Queue<T>is first in, first out (FIFO): values come back in the order they were added. Enqueueadds to the back,Dequeueremoves and returns the front, andPeekreturns the front without removing it.DequeueandPeekon an empty queue throw anInvalidOperationException, so checkCountfirst or useTryDequeue.- Use a queue when work should be handled in the order it arrived, such as customers in line or a waiting list.
A Queue<T> works like a line at a café counter: the first person to join the line is the first one served. That's first in, first out, or FIFO.
You don't reach into a queue by position. You add at the back, and take from the front.
Enqueue and Dequeue
Queue<string> line = new Queue<string>();
line.Enqueue("Emily");
line.Enqueue("Lucas");
line.Enqueue("Sophia");
Console.WriteLine( quot;Next up: {line.Peek()}");
while (line.Count > 0)
{
string customer = line.Dequeue();
Console.WriteLine( quot;Serving {customer}");
}
Next up: Emily
Serving Emily
Serving Lucas
Serving Sophia
| Member | Does |
|---|---|
Enqueue(value) |
Adds the value to the back of the line |
Dequeue() |
Removes the value at the front, and returns it |
Peek() |
Returns the value at the front, without removing it |
Count |
How many values are waiting |
TryDequeue(out value) |
Like Dequeue, but returns false instead of failing when the queue is empty |
An empty queue
Dequeue() and Peek() on an empty queue throw an InvalidOperationException:
Queue<string> line = new Queue<string>();
string next = line.Dequeue();
Unhandled exception. System.InvalidOperationException: Queue empty.
So check Count first, as the while loop above does, or use TryDequeue, which returns false when there's no one waiting:
Queue<string> waitingList = new Queue<string>();
waitingList.Enqueue("Mason");
for (int seat = 1; seat <= 2; seat++)
{
if (waitingList.TryDequeue(out string? name))
{
Console.WriteLine( quot;Seat {seat} goes to {name}");
}
else
{
Console.WriteLine( quot;Seat {seat} is open: nobody is waiting");
}
}
Seat 1 goes to Mason
Seat 2 is open: nobody is waiting
Where queues fit
Use a queue whenever work should be handled in the order it arrived: customers in line, orders waiting to be made, emails waiting to be sent, a waiting list for a sold-out show.
Do it: serve the next customers in line
The café serves customers in the order they joined the line. When the barista has a few drinks ready, the people at the front get them, and everyone else keeps their place.
Serve gets two values:
line: the customers waiting, aQueue<string>, with the first to arrive at the front.howMany: how many drinks are ready, anint(2).
For example, four customers are waiting, with Emily at the front, and 2 drinks are ready:
Queue<string> line = new Queue<string>(new[] { "Emily", "Lucas", "Sophia", "Mason" });
List<string> served = Serve(line, 2);
(new Queue<string>(new[] { ... }) makes a queue with those names already in line, the first one at the front.)
| Drink | Goes to | Still in line |
|---|---|---|
| 1st | Emily | Lucas, Sophia, Mason |
| 2nd | Lucas | Sophia, Mason |
So served is ["Emily", "Lucas"], and Sophia is now at the front of line.
Your job: write the method Serve(Queue<string> line, int howMany) so it returns the names of the customers served, a List<string>, in the order they were served.
- Take each customer served from the front of
line, so they leave the line. - Serve
howManycustomers, or fewer if the line runs out first. - The customers not served stay in
line, in their order. - When the line is empty, serve nobody: return an empty list, with no error.
Practiced live on the lesson page: write the C# right there and check it.
Check yourself
1 question to try on the lesson page.
So what
A queue makes "first come, first served" automatic: nobody can cut in line, and nobody gets skipped. When the order things arrive in is the order they should be handled, use a queue.