February 2019
Intermediate to advanced
626 pages
15h 51m
English
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 ...Read now
Unlock full access