ASP.NET Queue

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
VB.Net
Dim qu As New Queue qu.Enqueue("Sunday")
C#
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
VB.Net
qu.Dequeue()
C#
qu.Dequeue();

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

Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml"> <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

Conclusion

Through Queue class, developers manage well the needs of the scenarios that do not allow more wavering than FIFO line. Queue may play roles such as Printer spooler job management, Message processing in a message system, or implementing breadth-first Search among other tasks, Queue offers a clean and consistent way to regulate entering and exiting of the objects.