How to VB.Net Queue

The Queue is a fundamental data structure within VB.NET Collections. It operates based on the First In First Out (FIFO) principle, meaning that the item that is added first to the Queue is also the first one to be removed. In Queue, we can enqueue (add) items, dequeue (remove) items, or peek (retrieve the reference to) the first item added to the Queue.

The commonly using functions are follows :

The Queue class provides several commonly used functions for working with queues. Here are some examples of these functions:

Enqueue Method

The Enqueue method is used to add an item to the end of the queue. It takes an argument representing the item to be added.

Dim queue As New Queue() queue.Enqueue("Apple") queue.Enqueue("Banana") queue.Enqueue("Orange")

Dequeue Method

The Dequeue method is used to remove and retrieve the item at the beginning of the queue. It does not take any arguments.

Dim queue As New Queue() queue.Enqueue("Apple") queue.Enqueue("Banana") queue.Enqueue("Orange") Dim firstItem As String = queue.Dequeue() Console.WriteLine(firstItem) ' Output: Apple

Peek Method

The Peek method is used to retrieve the item at the beginning of the queue without removing it. It does not take any arguments.

Dim queue As New Queue() queue.Enqueue("Apple") queue.Enqueue("Banana") queue.Enqueue("Orange") Dim firstItem As String = queue.Peek() Console.WriteLine(firstItem) ' Output: Apple

Count Property

The Count property returns the number of items in the queue.

Dim queue As New Queue() queue.Enqueue("Apple") queue.Enqueue("Banana") queue.Enqueue("Orange") Dim itemCount As Integer = queue.Count Console.WriteLine(itemCount) ' Output: 3

The provided VB.NET source code showcases a selection of frequently utilized functions.

Full Source VB.NET
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles Button1.Click Dim queueList As New Queue queueList.Enqueue("Sun") queueList.Enqueue("Mon") queueList.Enqueue("Tue") queueList.Enqueue("Wed") queueList.Enqueue("Thu") queueList.Enqueue("fri") queueList.Enqueue("Sat") MsgBox(queueList.Dequeue()) MsgBox(queueList.Peek()) If queueList.Contains("Sun") Then MsgBox("Contains Sun ") Else MsgBox("Not Contains Sun ") End If End Sub End Class