C# - SPLessons

How to use C# Queue Class

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

How to use C# Queue Class

How to use C# Queue Class

Hi Friends, let me give you a intro about "Queue". It is a First-in-first-out (FIFO) collection of objects. When you add an item in the list, it is called Enqueue and when you remove it, it is called Dequeue. We will see How to use C# Queue Class.

Namespace :
[csharp] using System.Collections; [/csharp]

Step1 :
Please open visual studio, right now i have visual studio 2013 i just opened my visual studio and click on new project.

[image url=””]/wp-content/uploads/2014/09/c-Queue-Class-splessons1.png[/image]

Step2 :
In the new project window please click on templates visual c# and then click on windows  and on the right side panel just click on console application. Please give a name for the console application and click ok button.

[image url=””]/wp-content/uploads/2014/09/c-Queue-Class-splessons2.png[/image]

Step3 :
If you see the visual studio will gives you a predefined template program  for us.

[image url=””]/wp-content/uploads/2014/09/c-Queue-Class-splessons3.png[/image]

Step4 :
Lets, Build simple console application to Enqueue, Dequeue and  Display items from Queue.

Main Method :
[csharp] public static Queue queuetable = new Queue(); public static Boolean flag = true; static void Main(string[] args) { while (flag) { Console.WriteLine("\n1. Insert \t2. Remove \n3. Display\t4. Exit"); Console.Write("Please enter your Choice: "); string choice = Console.ReadLine(); switch (choice) { case "1": queuetable = Insert(queuetable); break; case "2": queuetable = Remove(queuetable); break; case "3": Display(queuetable); break; case "4": flag = false; break; default: Console.WriteLine("Enter correct choice !!"); break; } } } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/c-Queue-Class-splessons4.png[/image]

Enqueue :
[csharp] private static Queue Insert(Queue queuetable) { Console.Write("Please enter data into Queue : "); string queueValue = Console.ReadLine(); queuetable.Enqueue(queueValue); Display(queuetable); return queuetable; } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/c-Queue-Class-splessons5.png[/image]

Dequeue :
[csharp] private static Queue Remove(Queue queuetable) { string queueValue =queuetable.Dequeue().ToString(); Console.WriteLine("Object remove from Queue : " + queueValue); Display(queuetable); return queuetable; } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/c-Queue-Class-splessons6.png[/image]

Display :
[csharp] private static void Display(Queue queuetable) { Console.WriteLine("Items in the Queue : "); foreach (Object item in queuetable) { Console.WriteLine(item); } } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/c-Queue-Class-splessons7.png[/image]

Other Methods :
There are many other methods which you can try.

Count :
Gets the number of elements contained in the Queue.

Clone :
Creates a shallow copy of the Queue.

Peek :
Returns the object at the top of the Queue without removing it.

CopyTo :
Returns an Queue to an existing one-dimensional Array.

Clear :
Removes all elements from the Queue. etc.

Enjoy to Code..;)