NamespaceSystem.Collections (standard version) System.Collections.Generic (generic version) CreatableYes DescriptionA Queue object implements a "first in, first out" (FIFO) data structure. Items are added in a line (queue), with new items placed at the end of the line. Only the item at the beginning of the line can be removed. Its real-world counterpart is a line for the ticket counter at a movie theater. The queue includes features for adding items (Enqueue), removing items (Dequeue), and counting the items in the queue (Count), among other features. Objects of any type may be added to the queue. The following table lists some of the more useful and interesting members of the Queue class. Those marked with an asterisk (*) have separate entries in this chapter.
ExampleThis sample code shows the basic use of the queue. ' ----- Add some basic items to a queue. Dim nameQueue As New Queue nameQueue.Enqueue("Chopin") nameQueue.Enqueue("Mozart") nameQueue.Enqueue("Beethoven") ' ----- Examine and return the items. MsgBox(nameQueue.Peek( )) ' Displays "Chopin" MsgBox(nameQueue.Dequeue( )) ' Displays "Chopin" MsgBox(nameQueue.Dequeue( )) ' Displays "Mozart" ' ----- Remove the remaining items. MsgBox(nameQueue.Count) ' Displays 1 (for Beethoven) nameQueue.Clear( ) Version DifferencesVisual Basic 2005 adds support for generics to several collection-style classes, including the Queue class. The version of the Queue class that supports generics appears in the System.Collections.Generic namespace. Generics are discussed in Chapter 10. See AlsoCollection Class, Hashtable Class, Stack Class |