Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Tip/Trick

Queues Implemented with C#

Rate me:
Please Sign up or sign in to vote.
2.00/5 (4 votes)
12 Sep 2011CPOL 27K   3   12
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:

C#
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:

C#
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)


Written By
Software Developer Standout IT Solutions(P) Ltd.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 This is the very basic, and useless. Pin
intrueder10-Oct-11 8:35
intrueder10-Oct-11 8:35 
GeneralReason for my vote of 1 I give you a 1 because there is no w... Pin
Yet Another XCoder21-Sep-11 3:16
Yet Another XCoder21-Sep-11 3:16 
GeneralReason for my vote of 1 Plagarism Pin
fjdiewornncalwe12-Sep-11 9:25
professionalfjdiewornncalwe12-Sep-11 9:25 
GeneralReason for my vote of 5 Good job.... Pin
Pravin Patil, Mumbai12-Sep-11 8:21
Pravin Patil, Mumbai12-Sep-11 8:21 
GeneralReason for my vote of 1 MSDN online doc is better. http://ms... Pin
mattypiper21-Dec-10 7:48
mattypiper21-Dec-10 7:48 
GeneralReason for my vote of 1 Do not need MSDN copy Pin
Bad code hunter20-Dec-10 20:20
Bad code hunter20-Dec-10 20:20 
GeneralReason for my vote of 5 My Vote 5 Pin
Abdul Quader Mamun17-Dec-10 5:13
Abdul Quader Mamun17-Dec-10 5:13 
GeneralReason for my vote of 2 Good basic article Pin
Ravi Teja P16-Dec-10 17:48
Ravi Teja P16-Dec-10 17:48 
GeneralGood work Pin
Abdul Quader Mamun16-Dec-10 8:36
Abdul Quader Mamun16-Dec-10 8:36 
GeneralYou forgot to quote your source http://csharp.codefetch.com/... Pin
Indivara16-Dec-10 3:36
professionalIndivara16-Dec-10 3:36 
GeneralReason for my vote of 5 Good Tip. Pin
TweakBird16-Dec-10 0:26
TweakBird16-Dec-10 0:26 
GeneralReason for my vote of 5 good work Pin
ershad1215-Dec-10 16:53
ershad1215-Dec-10 16:53 

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

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