C# Non-Generic Queue

In C# there is both a generic and a non-generic queue. A queue can be described as a first-in first-out data structure, or FIFO for short. New elements are added to the bottom or end of the queue and the item at the front of the queue is available to be removed first. The non-generic version, discussed here, does not require a data type to be specified for the elements contained within, so there can be a mixture of types if desired.

The example below shows the declaration of a non-generic queue, with four items added to it using the 'Enqueue' method. In this case, all of the items added are string values.

Queue people = new Queue();

people.Enqueue("Bob Smith");
people.Enqueue("George Jones");
people.Enqueue("Fred Bloggs");
people.Enqueue("Alan White");

It is possible to see how many items there are in the queue using the ‘Count’ property of the queue itself.

Console.WriteLine(people.Count);

In order to see the items in a non-generic queue, without actually removing them, a ‘foreach’ loop can be used.

foreach (string person in people)
{
    Console.WriteLine(person);
}

The output is shown below. Notice that the order of the output mirrors how the items are added, with the element at the top being the first that will be removed.

Bob Smith
George Jones
Fred Bloggs
Alan White

It is also possible to view the item at the front of the queue, without actually removing it, using the 'Peek' method.

Console.WriteLine(people.Peek());

If it is necessary to find out if a particular value exists within a non-generic queue then the 'Contains' method can be used. The result of this returns a Boolean 'True' or 'False' value.

Console.WriteLine(people.Contains("George Jones"));

In order to remove individual items from a queue, the ‘Dequeue’ method can be utilised. The example below assigns the number of items in the queue to a variable. This value is then used to limit the number of iterations in a ‘for’ loop. Within the loop the ‘Dequeue’ method is used to remove an item from the queue, display it in the console and output the number of items left in the queue.

Queue people = new Queue();

people.Enqueue("Bob Smith");
people.Enqueue("George Jones");
people.Enqueue("Fred Bloggs");
people.Enqueue("Alan White");

int queueLength = people.Count;

Console.WriteLine("Items in queue: {0}", queueLength);
Console.WriteLine();

for (int i = 1; i <= queueLength; i++)
{

    Console.WriteLine("Next item from queue: {0}", people.Dequeue());
    Console.WriteLine("Items left in queue: {0}", people.Count);
    Console.WriteLine();

}

The resulting output is shown below.

Items in queue: 4

Next item from queue: Bob Smith
Items left in queue: 3

Next item from queue: George Jones
Items left in queue: 2

Next item from queue: Fred Bloggs
Items left in queue: 1

Next item from queue: Alan White
Items left in queue: 0

Finally, if it is necessary to remove all items in a queue in one go, instead of individually with the 'Dequeue' method, there is a ‘Clear’ method to accomplish this.

people.Clear();

Further Reading