Most .Net developers are familiar with the ArrayList collection. The ArrayList has become a stable in most .Net applications, replacing in most cases the array. But did you know there are other types of collections?
This will begin a series discussing some of the other types of collections available in C#, and how to use them. Most are pretty straight forward, it’s just a matter of getting to know them. We’ll start things off with the queue.
A queue is a type of first in – first out, or FIFO collection. Unlike an array list, you must read things in order. Queues are very structured, you can’t go sifting or randomly plucking data out of the middle of the collection. OK, I can see you out there, scratching your head and asking “well why would I want to use a queue if it’s not as flexible as an arraylist?”
In a way you’ve answered your own question. Because queues are very structured, you can ensure that your application (or consumers of your application) can’t violate your intent.
Queue’s are simple, they have a grand total of three methods and one property. The property, Count, is almost self explanatory. It tells you how many items are in the Queue.
To get data into the queue, use the Enqueue method. Using the Dequeue method will return the next item, removing it from the collection. What if you want to see what the next item is, but don’t want to remove it? You can use the final method, Peek. Let’s take a look at a sample, done as a Console application.
Queue myQueue = new Queue();
myQueue.Enqueue(“1. Carl Franklin”);
myQueue.Enqueue(“2. Richard Campbell”);
myQueue.Enqueue(“3. Mark Miller”);
myQueue.Enqueue(“4. Kimberly Tripp”);
myQueue.Enqueue(“5. Billy Hollis”);
Console.WriteLine(“Count = “ + myQueue.Count.ToString());
while (myQueue.Count > 0)
{
Console.WriteLine(myQueue.Dequeue());
}
Console.WriteLine(“Count = “ + myQueue.Count.ToString());
Console.ReadLine();
This routine prints the following output to the command window:
As you can see, the Dequeue method returned the items in the same order they were loaded in. There’s really not much to explain, you load objects into the queue using Enqueue (in this case strings), and remove objects using Dequeue.
I also mentioned a Peek method. This allows you to look at the next item without removing it. Here I’ve modified the while loop to first show the next item on the queue using peek, then actually remove it.
while (myQueue.Count > 0)
{
Console.WriteLine(“The next DNR person is:” + myQueue.Peek());
Console.WriteLine(“Dequeuing them……..:” + myQueue.Dequeue());
}
There are several instances where you may want to use a queue over an array list. Perhaps you are processing messages, or are reading through a list where it’s important you read them in order, say a list of transactions from a database. Data that needs to be processed chronologically could be placed on a queue.
Any time you have data that must be processed in the order in which it was loaded, use a queue. The rigidity of the queue will add an extra layer of safety to your application, ensuring data is read in the order in which you intended.
yes this is about the best i have seen on counting items in a array without getting confussed, great.
I agree with david, this is one the best and simplest way to understand how easy it is to work with queue’s in c#.
Thanks.
Great, happy for finding this… thanks.
That’s an all ’round good article!!!
This surely makes great sense!
Realy great article thanks…..