Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is iterating over a Queue<T> guaranteed to be in queue order?

Is this guaranteed to always print 123?

Queue<string> theQueue = new Queue<string>();
theQueue.Enqueue("1");
theQueue.Enqueue("2");
theQueue.Enqueue("3");
foreach(var str in theQueue)
{
    Console.Write(str);
}
Console.WriteLine();

Edit: I totally agree that a queue that enumerated in any other order would be obviously incorrect. That's why I asked the question. However, the abstract data type queue only makes guarantees about its enqueue and dequeue operations.

I'm looking for an answer that references documentation that guarantees this ordering in the .NET BCL.

like image 749
Jacob Krall Avatar asked Oct 16 '25 04:10

Jacob Krall


1 Answers

Yes. It is. A queue is a first-in, first-out collection and will be enumerated in order.

like image 51
Ant P Avatar answered Oct 19 '25 09:10

Ant P