Like it

Sunday, July 10, 2011

Queue in C# / Add and Remove items from Queue in C# (.NET)

Queues are common in real world - Flight checkins to School. Here is the way you can use it in C#

private static void CustomerServiceQueue() 
{ 
Queue CSQ = new Queue(); 
CSQ.Enqueue("Sid"); 
CSQ.Enqueue("Nad"); 
CSQ.Enqueue("Am"); 
CSQ.Enqueue("Sam"); 
Console.WriteLine("Peeking Queue: {0}", CSQ.Peek()); 
Console.WriteLine("Check if CSQ Contains Sid: {0}", CSQ.Contains("Sid"));
while (CSQ.Count > 0) 
{ 
Console.WriteLine(CSQ.Dequeue()); 
} 
}
Contains checks for presence of the given object in the queue. Peek returns the first element from the queue

Dequeuue does the same as Peek, however, it also removes the object


No comments:

Post a Comment