How it works
A queue removes items in the order they were enqueued. It is a natural fit for a prepared worklist when earlier arrivals should be processed first. The loop checks Count before Dequeue, avoiding an empty-queue exception. Adding another item during processing places it behind the items already waiting. This collection does not provide persistence, retries, or multi-process coordination; those are properties of a larger job system.
A simple Queue<T> is also not a general thread-safe shared queue. In this single execution it provides precisely the ordering operation the algorithm requires without introducing unrelated infrastructure.
Replace the queue with a stack and predict the different order. Decide whether that change still matches an arrival-order contract before considering it an implementation detail.
A working example
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
var queue = new Queue<string>();
queue.Enqueue("first"); queue.Enqueue("second");
while (queue.Count > 0)
{
var item = queue.Dequeue();
Console.WriteLine(item);
if (item == "first") queue.Enqueue("third");
}
Expected output
first second third
New work goes behind work that is already waiting.