Click here to Skip to main content
11,479,096 members (57,114 online)
Click here to Skip to main content

Tagged as

Queues Implemented with C#

, 12 Sep 2011 CPOL 13K 3
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. Pin
InTRUEdeR10-Oct-11 9:35
memberInTRUEdeR10-Oct-11 9:35 
GeneralReason for my vote of 1 I give you a 1 because there is no w... Pin
rmiguel21-Sep-11 4:16
memberrmiguel21-Sep-11 4:16 
GeneralReason for my vote of 1 Plagarism Pin
Marcus Kramer12-Sep-11 10:25
memberMarcus Kramer12-Sep-11 10:25 
GeneralReason for my vote of 5 Good job.... Pin
Pravin Patil, Mumbai12-Sep-11 9:21
memberPravin Patil, Mumbai12-Sep-11 9:21 
GeneralReason for my vote of 1 MSDN online doc is better. http://ms... Pin
mattypiper21-Dec-10 8:48
membermattypiper21-Dec-10 8:48 
GeneralReason for my vote of 1 Do not need MSDN copy Pin
_FleX20-Dec-10 21:20
member_FleX20-Dec-10 21:20 
GeneralReason for my vote of 5 My Vote 5 Pin
Abdul Quader Mamun17-Dec-10 6:13
memberAbdul Quader Mamun17-Dec-10 6:13 
GeneralReason for my vote of 2 Good basic article Pin
Ravi Teja P16-Dec-10 18:48
memberRavi Teja P16-Dec-10 18:48 
GeneralGood work Pin
Abdul Quader Mamun16-Dec-10 9:36
memberAbdul Quader Mamun16-Dec-10 9:36 
GeneralYou forgot to quote your source http://csharp.codefetch.com/... Pin
Indivara16-Dec-10 4:36
subeditorIndivara16-Dec-10 4:36 
GeneralReason for my vote of 5 Good Tip. Pin
Eswa16-Dec-10 1:26
memberEswa16-Dec-10 1:26 
GeneralReason for my vote of 5 good work Pin
ershad1215-Dec-10 17:53
memberershad1215-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
Web04 | 2.8.150520.1 | Last Updated 12 Sep 2011
Article Copyright 2011 by SuhasHaridas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid