Queues

A queue
represents a first-in, first-out (FIFO) collection. The classic
analogy is to a line (or queue if you are British) at a ticket
window. The first person in line ought to be the first person to come
off the line to buy a ticket.

A queue is a good collection to use when you are managing a limited
resource. For example, you might want to send messages to a resource
that can handle only one message at a time. You would then create a
message queue so that you can say to your clients:
"Your message is important to us. Messages are
handled in the order in which they are received."

The Queue class has
a number of member methods and properties, as shown in Table 9-4.

Table 9-4. Queue methods and properties

Method or property

Purpose

Count

Public property that gets the number of elements in the
Queue.

Clear( )

Removes all objects from the Queue.

Contains( )

Determines if an element is in the Queue.

CopyTo( )

Copies the Queue elements to an existing
one-dimensional array.

Dequeue( )

Removes and returns the object at the beginning of the
Queue.

Enqueue( )

Adds an object to the end of the Queue.

GetEnumerator( )

Returns an enumerator for the Queue.

Peek( )

Returns the object at the beginning of the Queue
without removing it.

ToArray( )

Copies the elements to a new array.

Add
elements to your queue with the Enqueue command
and take them off the queue with Dequeue or by
using an enumerator. Example 9-16 illustrates.

In this example the List is replaced by a
Queue. I've dispensed with the
Employee class to save room, but of course you can
Enqueue user-defined objects as well.

The output shows that queuing objects adds them to the
Queue, and calls to Dequeue
return the object and also remove them from the
Queue. The Queue class also
provides a Peek() method that allows you to see,
but not remove, the first element.

Because the Queue class is enumerable, you can
pass it to the
PrintValues method, which is provided as an
IEnumerable interface. The conversion is
implicit. In the PrintValues method you call
GetEnumerator, which you will remember is the
single method of all IEnumerable classes. This
returns an
IEnumerator, which you then use to enumerate all
the objects in the collection.

Stacks

A stack
is a last-in, first-out (LIFO) collection, like a stack of dishes at
a buffet table, or a stack of coins on your desk. A dish added on top
is the first dish you take off the stack.

The principal methods for adding to and removing from a stack are
Push() and Pop();
Stack also offers a Peek()
method, very much like Queue. The significant
methods and properties for
Stack are
shown in Table 9-5.

Table 9-5. Stack methods and properties

Method or property

Purpose

Count

Public property that gets the number of elements in the
Stack.

Clear( )

Removes all objects from the Stack.

Clone( )

Creates a shallow copy.

Contains( )

Determines if an element is in the Stack.

CopyTo( )

Copies the Stack elements to an existing
one-dimensional array.

GetEnumerator( )

Returns an enumerator for the Stack.

Peek( )

Returns the object at the top of the Stack without
removing it.

Pop( )

Removes and returns the object at the top of the
Stack.

Push( )

Inserts an object at the top of the Stack.

ToArray( )

Copies the elements to a new array.

The List, Queue, and
Stack types contain overloaded
CopyTo( ) and ToArray( ) methods
for copying their elements to an array. In the case of a
Stack, the CopyTo( ) method will
copy its elements to an existing one-dimensional array, overwriting
the contents of the array beginning at the index you specify. The
ToArray( ) method returns a new array with the
contents
of the stack's elements. Example 9-17 illustrates.