why should we care about the system.collections.Queue collection
Working with powershell, generally a simple array declared and used like this will generally be enough.
1 2 3 |
$array = @() $array += "Stéphane" $array += "van Gulick" |
We then need to access our array via the index
1 2 |
PS C:\WINDOWS\system32> $array[1] van Gulick |
The System.Collection.queue follows the principle of first in, first out.
Methods and properties you don’t want to miss:
Lets have a look at the members of our collection object:
1 2 |
$q = New-Object System.Collections.Queue $q | gm # throws an error |
Pretty surprisingly, when you try to look at the members that are available you will find your self in front of the following error:
1 2 3 4 5 6 |
gm : You must specify an object for the Get-Member cmdlet. At line:1 char:6 + $q | gm # throws an error + ~~ + CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand |
I’ll explain a bit later what really happened here, but for now, know that although we have had this error message, and when we look into our $q variable it seems empty, the instance has been created. We will can see it members if we use the -inputObject parameter, as in the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
PS C:\WINDOWS\system32> get-member -InputObject $q TypeName: System.Collections.Queue Name MemberType Definition ---- ---------- ---------- Clear Method void Clear() Clone Method System.Object Clone(), System.Object ICloneable.Clone() Contains Method bool Contains(System.Object obj) CopyTo Method void CopyTo(array array, int index), void ICollection.CopyTo(array array, int index) Dequeue Method System.Object Dequeue() Enqueue Method void Enqueue(System.Object obj) Equals Method bool Equals(System.Object obj) GetEnumerator Method System.Collections.IEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator() GetHashCode Method int GetHashCode() GetType Method type GetType() Peek Method System.Object Peek() ToArray Method System.Object[] ToArray() ToString Method string ToString() TrimToSize Method void TrimToSize() Count Property int Count {get;} IsSynchronized Property bool IsSynchronized {get;} SyncRoot Property System.Object SyncRoot {get;} |
- Enqueue
- Dequeue
- peek
The .Enqueue() method of the system.collections.Queue
The enqueue method is the method that will allow us to add new information to our queue.
1 2 3 4 5 6 7 8 |
$q.Enqueue("hi") $q.Enqueue("How are") $q.Enqueue("you ?") $q hi How are you ? |
Notice the order in which the strings are appearing.
Pretty straight forward I would say.
The DeQueue() method
The dequeue method is the method that will return the next element of our queue.
Remember, the systems.collections.Queue is a type of collection that respects the first in, First out principle
1 2 |
PS C:\WINDOWS\system32> $q.Dequeue() hi |
As you can see, “hi” is the first element to be returned when we called the DeQueue() method.
When we look again in our variable, we can see that the value hi is not more present anymore, but “how are” and “you” are still there.
1 2 3 |
PS C:\WINDOWS\system32> $q How are you ? |
The Peek() method:
The peek method will work exactly as the Dequeue() method, except, that the item that was returned will not be removed from the Queue. As it’s name suggest, it allows you to peek into the queue, and to see what would be the next returned element if you would call the DeQueue() method.
1 2 |
PS C:\WINDOWS\system32> $q.Peek() How are |
As you can see in the example above, the peek method (in red) returns the item, but doesn’t removes it from the stack collection.
Using the pop method, returns the “gulick” item just as the peek method informed us it would do, and in this case, removed it from the stack item.
Nice article I’ll for sure book mark this for later use.