Author - Dave Amour

Dave Amour has used computers for as long as he can remember and intially started out as an IT trainer delivering a range of IT courses but for the last 11 years has been focusing on the arena of web application development. He was worked for numerous companies over the years and is currently working for Audacs Software Ltd. Dave is also a keen squash player and an active and sucessful member of Experts Exchange

Please feel free to submit any constructive comments which you can do at the bottom of this page.

Dave may be available for programming tuition or consultancy work. Contact via dave@audacs.co.uk
Dave Amour - Click to view CV 

Collection Classes in .net - the Queue

This is the seventh part of an article on collection classes in .net with C#.  This part covers the Queue.

For the first part see http://www.audacs.co.uk/ViewPage.aspx?PageID=512

For the next part on the LinkedList see http://www.audacs.co.uk/ViewPage.aspx?PageID=520

The queue is very simillar to the stack.  the key difference is that this is a FIFO structure.   FIFO stands for first in first out.  This class has been created to help us model real world scenarios where such behaviour is required.  For example a printer queue.

Like the stack there are two versions of this class. The original version and a Generic version.  These can be found at:

System.Collections.Queue
System.Collections.Generic.Queue

You can place any object at all in the queue since it is defined to hold a collection of objects.  The generic versions of course also requires that you define this whereas the non generic version requires some casting within your code.

Lets have a look at a simple program which shows us how we can use the queue.  Note that we use an Enqueue method to add something to the queue and we use a Dequeue method to remove an item.

Below we will show the code for a version using the original queue and then one using the generic version.  The output from both which is shown afterwards is identical in both cases.

Original Version

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Queue
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Collections.Queue people = new System.Collections.Queue();
 
            people.Enqueue("Fred");
            people.Enqueue("Dave");
            people.Enqueue("Sarah");
            people.Enqueue("Lisa");
            people.Enqueue("Bill");
 
            foreach (string person in people)
            {
                Console.WriteLine(person);
            }
 
            string dequeuedPerson = (string)people.Dequeue();
 
            Console.WriteLine();
 
            Console.WriteLine(dequeuedPerson);
 
            Console.Read();
        }
    }
}
 


Generic Version
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Queue
{
    class GenericQueue
    {
        static void Main(string[] args)
        {
            System.Collections.Generic.Queue<string> people = new System.Collections.Generic.Queue<string>();
 
            people.Enqueue("Fred");
            people.Enqueue("Dave");
            people.Enqueue("Sarah");
            people.Enqueue("Lisa");
            people.Enqueue("Bill");
 
            foreach (string person in people)
            {
                Console.WriteLine(person);
            }
 
            string dequeuedPerson = people.Dequeue();
 
            Console.WriteLine();
 
            Console.WriteLine(dequeuedPerson);
 
            Console.Read();
        }
    }
}
 


So the only real difference is that we declare the queue as a generic type and then when we do a Dequeue then there is no casting involved.

The output from both programs looks like this:

 

And thats about it for the Queue. For the next part on the LinkedList see http://www.audacs.co.uk/ViewPage.aspx?PageID=520

Leave a comment

Name

Email (optional and not disclosed)



Email address is not disclosed but just used to alert you of new posts if entered
Word CV
HTML CV
PDF CV
Text CV
CMS Lite
ETraining
Planet Health
Florida Health
the Date Shack
Taylors
Emcat
Emtex
Browne Jacobson
Alliance & Leicester
Baird Leisure
Swarfega
Creature Comforts
Rugeley Chess Club
Katnip
Katmaid
Dudley NHS
Contact Me
Current Status