A queue is a data structure that allows adding elements at one end of the queue, through Enqueue operations, and removing elements from the other end, through Dequeue operations.
The .NET Framework includes a Queue<T> type, defined in System.Collections.Generic, that implements a queue data type.
When you declare a queue, you indicate the type of objects that the queue will contain.
1 | Queue<Person> peopleInLine = new Queue<Person>(); |
You can now add instances of Person objects to the (back of) the queue using the Enqueue method.
1 2 3 4 5 | // Ernest - 1st guy in linepeopleInLine.Enqueue(new Person("Ernest", "Hemingway"));// Scott in line behind ErnestpeopleInLine.Enqueue(new Person("F. Scott", "Fitzgerald")); |
You can remove the object from the front of the queue using the Dequeue method.
1 2 | // Remove guy from front of line -- ErnestPerson next = peopleInLine.Dequeue(); |

