.Net application development specialists
asp.net, c#, vb.net, html, javascript, jquery, html, xhtml, css, oop, design patterns, sql server, mvc and much more
contact: admin@paxium.co.uk

Paxium is the company owned by myself, Dave Amour and used for providing IT contract development services including


  • Application development - Desktop, Web, Services - with Classic ASP, Asp.net WebForms, Asp.net MVC, Asp.net Core
  • Html, Css, JavaScript, jQuery, React, C#, SQL Server, Ado.net, Entity Framework, NHibernate, TDD, WebApi, GIT, IIS
  • Database schema design, implementation & ETL activities
  • Website design and hosting including email hosting
  • Training - typically one to one sessions
  • Reverse Engineering and documentation of undocumented systems
  • Code Reviews
  • Performance Tuning
  • Located in Cannock, Staffordshire
Rugeley Chess Club Buying Butler Cuckooland Katmaid Pet Sitting Services Roland Garros 60 60 Golf cement Technical Conformity Goofy MaggieBears Vacc Track Find Your Smart Phone eBate Taylors Poultry Services Lafarge Rebates System Codemasters Grid Game eBate DOFF

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