15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Queue ❘ 237<br />

Figure 10-1 shows the items of the queue. The Enqueue() method adds items to one end of the queue;<br />

the items are read <strong>and</strong> removed at the other end of the queue with the Dequeue() method. Invoking the<br />

Dequeue() method once more removes the next item from the queue.<br />

Enqueue<br />

figure 10-1<br />

Dequeue<br />

Methods of the Queue class are described in the following table.<br />

seleCTed queue members<br />

Count<br />

Enqueue()<br />

Dequeue()<br />

Peek()<br />

TrimExcess()<br />

desCriPTion<br />

The property Count returns the number of items in the queue.<br />

The Enqueue() method adds an item to the end of the queue.<br />

The Dequeue() method reads <strong>and</strong> removes an item from the head<br />

of the queue. If there are no more items in the queue when the<br />

Dequeue() method is invoked, an exception of type<br />

InvalidOperationException is thrown.<br />

The Peek() method reads an item from the head of the queue but<br />

does not remove the item.<br />

TrimExcess() resizes the capacity of the queue. The Dequeue()<br />

method removes items from the queue, but it doesn’t resize the capacity<br />

of the queue. To get rid of the empty items at the beginning of the<br />

queue, use the TrimExcess() method.<br />

When creating queues, you can use constructors similar to those used with the List type. The default<br />

constructor creates an empty queue, but you can also use a constructor to specify the capacity. As items are<br />

added to the queue, the capacity is increased to hold 4, 8, 16, <strong>and</strong> 32 items if the capacity is not defined.<br />

Similarly to the List class, the capacity is always doubled as required. The default constructor of the<br />

non-generic Queue class is different, because it creates an initial array of 32 empty items. With an overload<br />

of the constructor, you can also pass any other collection that implements the IEnumerable interface<br />

that is copied to the queue.<br />

The sample application that demonstrates the use of the Queue class is a document management application.<br />

One thread is used to add documents to the queue, <strong>and</strong> another thread reads documents from the<br />

queue <strong>and</strong> processes them.<br />

The items stored in the queue are of type Document. The Document class defines a title <strong>and</strong> content:<br />

public class Document<br />

{<br />

public string Title { get; private set; }<br />

public string Content { get; private set; }<br />

}<br />

public Document(string title, string content)<br />

{<br />

this.Title = title;<br />

this.Content = content;<br />

}<br />

code snippet QueueSample/Document.cs<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!