The Queue class operates on the fundamental principle of first-in, first-out (FIFO), creating a highly efficient and orderly collection of Objects. In essence, it follows a sequential order where the first item enqueued is the first item dequeued, ensuring a fair and predictable data management system.

Populate the Queue

To populate the Queue, we employ the Enqueue operation, which adds items to the rear end of the collection. This ensures that newer elements are appended to the end, maintaining the integrity of the FIFO sequence. Conversely, when it comes to retrieval, the Dequeue operation removes and returns the front-most item from the Queue. This systematic approach guarantees that the oldest elements are processed first, in alignment with the FIFO principle.

In addition to enqueueing and dequeuing, the Queue class provides a convenient Peek operation. This operation allows us to inspect the reference of the front-most item in the Queue without removing it. By peering into the Queue, developers can access vital information about the next element to be dequeued, facilitating informed decision-making and efficient data processing.

Queue.Enqueue(Object) : Add an Item in the Queue
Dim qu As New Queue qu.Enqueue("Sunday")
Queue qu = new Queue(); qu.Enqueue("Sunday");

It is worth noting that the Queue class accepts null references as valid values, allowing for the inclusion of empty elements within the collection. Furthermore, duplicate elements are also accommodated, ensuring flexibility in managing data sets that may contain identical values.

Object Queue.Dequeue() : Remove the oldest item from the Queue

The following ASP.NET program add seven days in week to a Queue and bind it to a ListBox control.

<html xmlns=""> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <br /> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox><br /> </div> </form> </body> </html>
Full Source | C#
using System; using System.Collections; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { Queue qu = new Queue(); qu.Enqueue("Sunday"); qu.Enqueue("Monday"); qu.Enqueue("Tuesday"); qu.Enqueue("Wednesday"); qu.Enqueue("Thursday"); qu.Enqueue("Friday"); qu.Enqueue("Saturday"); ListBox1.DataSource = qu; ListBox1.DataBind(); } }
Full Source | VB.NET
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim qu As New Queue qu.Enqueue("Sunday") qu.Enqueue("Monday") qu.Enqueue("Tuesday") qu.Enqueue("Wednesday") qu.Enqueue("Thursday") qu.Enqueue("Friday") qu.Enqueue("Saturday") ListBox1.DataSource = qu ListBox1.DataBind() End Sub End Class


By using the Queue class, developers can effectively handle scenarios that demand a strict adherence to the FIFO system. Whether it be managing jobs in a printer spooler, processing messages in a messaging system, or implementing breadth-first search algorithms, the Queue provides an elegant and reliable solution for maintaining the order of objects as they enter and exit the collection.