A Queue is a collection of First-In-First-Out (FIFO) (this is explicitly stated in the documentation). This means that the enumeration provides you with the elements in the order in which they were added.
A Stack is the Last-In-First-Out (LIFO) collection. This means that the enumerator provides you with the elements in the reverse order of how they were added.
Stacks and queues are a very standard Computer Science construct, so they really cannot be reconfigured without a serious reaction. When you look at the GetEnumerator() function examples, it clearly documents the order of enumeration:
Stack Enumeration:
Stack<string> numbers = new Stack<string>(); numbers.Push("one"); numbers.Push("two"); numbers.Push("three"); numbers.Push("four"); numbers.Push("five");
Queuing enumeration:
Queue<string> numbers = new Queue<string>(); numbers.Enqueue("one"); numbers.Enqueue("two"); numbers.Enqueue("three"); numbers.Enqueue("four"); numbers.Enqueue("five");
Again, with the basic definitions of computer science, an enumerator or iterator should present elements in a natural way for a collection. Specific collection types have a specific order.
Caveat
Note that although the listing process reflects the natural order of the FIFO and LIFO ( ref ) collections, it is not the way Queues ( ref ) and Stacks ( ref ) are intended to be used, They are intended to be used with the Enqueue() / Dequeue() and Push() / Pop() / Peek() . Microsoft includes an enumeration so that everything is consistent with the ICollection<T> base interface and maintains the numbering in the natural order of the collection.
The purpose of the queue is to create a work pipeline that can be processed in order. The purpose of the stack is to provide a way to return to the previous context when local work is performed. They are designed to work on one subject at a time. Iterating over a collection using an enumerating view of side steps that do not completely remove elements from the queue / stack. This is essentially a look at all the objects that are.
Berin loritsch
source share