BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

The Queue Collection

The fortieth part of the C# Fundamentals tutorial describes the use of the Queue class. This collection includes the functionality required in a 'First In, First Out' (FIFO) queuing structure. Queues allow items to be held in order for later processing.

Peek Method

You may wish to obtain the item at the front of the queue without modifying the collection's contents. The Peek method is similar to Dequeue but when the next item is retrieved, it also remains at the front of the queue.

Queue waiting = new Queue();

waiting.Enqueue("Mrs Brown");
waiting.Enqueue("Mr Green");
waiting.Enqueue("Miss Black");

string next = waiting.Peek().ToString();
Console.WriteLine(next);                            // Outputs "Mrs Brown"
Console.WriteLine("Count: {0}", waiting.Count);     // Outputs "Count: 3"

Other Queue Processing Methods

In addition to FIFO queue processing methods, the Queue class provides other functionality for accessing the collection. These enhance the data structure with behaviours similar to those of some other collection and list types.

Clear

The Clear method is used to empty the contents of a queue. The method requires no parameters.

Queue waiting = new Queue();

waiting.Enqueue("Mrs Brown");
waiting.Enqueue("Mr Green");
waiting.Enqueue("Miss Black");

Console.WriteLine("Count: {0}", waiting.Count);     // Outputs "Count: 3"
waiting.Clear();
Console.WriteLine("Count: {0}", waiting.Count);     // Outputs "Count: 0"

TrimToSize

As the number of items in a queue collection increases, the size of the queue can also increase. Using standard settings, each time the collection capacity is exceeded it is doubled. As the queue empties, its capacity is not lowered. This can lead to a large amount of wasted memory for larger queues. To reduce this, the queue can be trimmed in order to free up the memory allocated to empty space using the TrimToSize method.

As the Queue class does not include a property that returns the current capacity, it is difficult to show the effect of executing the TrimToSize method. However, the method can be used as shown in the following example:

Queue waiting = new Queue();

waiting.Enqueue("Mrs Brown");
waiting.Enqueue("Mr Green");
waiting.Enqueue("Miss Black");

waiting.TrimToSize();                               // Resize the queue

Contains

Sometimes you will need to determine if a specified item exists within a queue without dequeuing every item in order to find it. The Contains method solves this problem by returning a Boolean value indicating if the provided object is in the queue.

Queue waiting = new Queue();

waiting.Enqueue("Mrs Brown");
waiting.Enqueue("Mr Green");
waiting.Enqueue("Miss Black");

Console.WriteLine(waiting.Contains("Mrs Brown"));   // Outputs "True"
Console.WriteLine(waiting.Contains("Mrs White"));   // Outputs "False"

ToArray

A further method exists that permits a queue's contents to be read without disturbing the order of the items. In a similar manner to with an ArrayList, the contents of a queue can be extracted into an array of objects.

Queue waiting = new Queue();

waiting.Enqueue("Mrs Brown");
waiting.Enqueue("Mr Green");
waiting.Enqueue("Miss Black");

// Copy items
object[] array = waiting.ToArray();

// List array items
foreach (object o in array)
{
    Console.WriteLine(o.ToString());
}

/* OUTPUT

Mrs Brown
Mr Green
Miss Black

*/

Creating a Thread-Safe Queue Wrapper

In the earlier article discussing collection interfaces, the concept of a thread-safe synchronised collection was described. In order to create a thread-safe Queue, a synchronised wrapper collection is generated using the static Synchronized method.

Queue myCollection = new Queue();
Queue myThreadSafe = Queue.Synchronized(myCollection);
Console.WriteLine(myThreadSafe.IsSynchronized);	        // Outputs "True"
15 June 2007