Removing elements from the queue

Elements are removed from the end using the Dequeue method:

$queue.Dequeue()    # This returns Tom. 

If the queue is empty and the Dequeue method is called, an error will be thrown, as shown here:

PS> $queue.Dequeue()
Exception calling "Dequeue" with "0" argument(s): "Queue empty."
At line:1 char:1
+ $queue.Dequeue()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException

To avoid this, the Count property of the queue may be inspected, for example:

# Set-up the queue 
$queue = New-Object System.Collections.Generic.Queue[String] 
"Tom", "Richard", "Harry" | ForEach-Object { 
    $queue.Enqueue($_) 
} 
# Dequeue until the queue is empty 
while ($queue.Count -gt 0) { 
    Write-Host $queue.Dequeue() 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.134.154