Click here to Skip to main content
Click here to Skip to main content

Tagged as

Queues Implemented with C#

, 12 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Queues Implemeted with C#

Queues are just like stacks, except that the objects collected by them are first in, first out (FIFO).

First, a Queue object needs to be declared and instantiated:

Queue theQueue = new Queue();

Here's the code to enqueue a string:

private void btnEnqueue_Click(object sender, System.EventArgs e) {
theQueue.Enqueue(txtIn.Text);
txtIn.Text = "";
lstQ.Items.Clear();
foreach (string s in theQueue) {
lstQ.Items.Add(s);
}
}

Dequeuing is pretty much the same thing, although (as you'd suspect) a check needs to be added to see that there is actually something on the queue:

private void btnDequeue_Click(object sender, System.EventArgs e) {
if (theQueue.Count > 0) {
txtOut.Text = (string) theQueue.Dequeue();
lstQ.Items.Clear();
foreach (string s in theQueue) {
lstQ.Items.Add(s);
}
}
else
MessageBox.Show("Nothing on the queue to dequeue!",
"No more waiting in line!", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SuhasHaridas
Software Developer Standout IT Solutions(P) Ltd.
India India
My name is SuhasHaridas.
 
I'm a Software Engineer in the .NET Developer Platform group within the Developer Division at Standout IT Solutions(P) Ltd.
I have focused on the Web platform during these years.Specifically, that means I have worked on ASP.NET and c#.NET ,SharePoint,Sql, WCS Apps(Cisco Wireless Control System),.NET Compact Framework, LINQ , ADO.NET , Ado.net entity framework ,Silverlight , WCF Services , SQL SQLCE Visual Studio and Visual Studio Web Developer as well as on IIS7.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralReason for my vote of 1 This is the very basic, and useless. PinmemberInTRUEdeR10-Oct-11 9:35 
GeneralReason for my vote of 1 I give you a 1 because there is no w... Pinmemberrmiguel21-Sep-11 4:16 
GeneralReason for my vote of 1 Plagarism PinmemberMarcus Kramer12-Sep-11 10:25 
GeneralReason for my vote of 5 Good job.... PinmemberPravin Patil, Mumbai12-Sep-11 9:21 
GeneralReason for my vote of 1 MSDN online doc is better. http://ms... Pinmembermattypiper21-Dec-10 8:48 
GeneralReason for my vote of 1 Do not need MSDN copy Pinmember_FleX20-Dec-10 21:20 
GeneralReason for my vote of 5 My Vote 5 PinmemberAbdul Quader Mamun17-Dec-10 6:13 
GeneralReason for my vote of 2 Good basic article PinmemberRavi Teja P16-Dec-10 18:48 
GeneralGood work PinmemberAbdul Quader Mamun16-Dec-10 9:36 
GeneralYou forgot to quote your source http://csharp.codefetch.com/... PinsubeditorIndivara16-Dec-10 4:36 
GeneralReason for my vote of 5 Good Tip. PinmemberEswa16-Dec-10 1:26 
GeneralReason for my vote of 5 good work Pinmemberershad1215-Dec-10 17:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 12 Sep 2011
Article Copyright 2011 by SuhasHaridas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid