Search results for 'Enqueue'. 1 post(s) found.

  1. 2007/08/27 Queue Tutorial In C#
2007/08/27 08:31

Queue Tutorial In C#


Adding Items to the Queue
To add items to the Queue you use Enqueue method. This method takes an object of any type but to keep things simple lets just give it a string object:

Queue q = new Queue();

q.Enqueue("So");
q.Enqueue("This");
q.Enqueue("Is");
q.Enqueue("How");
q.Enqueue("Queues");
q.Enqueue("Work");


 

Viewing a Single Object Without Removing it From the Queue
You can use the Peak method to get the topmost object in the Queue without removing it from the Queue:

Console.WriteLine(q.Peek());


 

Viewing All the Objects in the Queue Without Removing Them
You can read any of the data in the Queue by using an enumerator:

System.Collections.IEnumerator en = q.GetEnumerator();

while (en.MoveNext())
{
   Console.Write(en.Current   " ");
}


 

Removing Items from the Queue
To pop an item from the Queue you can use the Dequeue statement. This returns the topmost object of the Queue.

while( q.Count > 0)
{
   Console.Write(q.Dequeue   " ");
}

Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 04:58 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 13:46 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:44 delete

    moneyideas