Working with Queue in C#

C# Queue class is a collection class that represents a queue. Learn how to use the C# Queue to implement queue functionality in C#.

A Queue represents a first-in, first-out (FIFO) collection of objects. An example of a queue is a line of people waiting.

The Queue<T> class in the System.Collection.Generic namespace represents a queue in C#, where T specifies the type of elements in the queue. In this article and code examples, I explain the constructor, properties and methods of the Queue class and how to use its various methods using C#.
 

Constructors 

 
The Queue<T> class has several overloaded constructors.
 
Queue<T> Constructor
 
Initializes a new instance of the Queue<T> class that is empty. The next lines shows how we create the empty queue.
  1. Queue<string> queue = newQueue<string>();  
Queue<T> Constructor (IEnumerable<T>)
 
Initializes a new instance of the Queue<T> class that contains elements copied from the specified collection.
  1. string[] courses = { "MCA","MBA""BCA","BBA""BTech","MTech" };  
  2. Queue<string> queue = newQueue<string>(courses);  
Queue<T> Constructor (Int32)
 
Initializes a new instance of the Queue<T> class that is empty and has the specified initial capacity.
  1. Queue<string> queue = newQueue<string>(4);  

Queue<T> Properties

 
Queue.Count Property
 
Gets the number of elements contained in the Queue<T>. The following code sample creates a Queue of strings and gets the item count in the collection.
  1. namespace Queue {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             string[] courses = {  
  5.                 "MCA",  
  6.                 "MBA",  
  7.                 "BCA",  
  8.                 "BBA",  
  9.                 "BTech",  
  10.                 "MTech"  
  11.             };  
  12.             Queue < string > queue1 = newQueue < string > ();  
  13.             Queue < string > queue2 = newQueue < string > (courses);  
  14.             Queue < string > queue3 = newQueue < string > (4);  
  15.             Console.WriteLine("Number of elements in queue1:" + queue1.Count());  
  16.             Console.WriteLine("Number of elements in queue2:" + queue2.Count());  
  17.             Console.WriteLine("Number of elements in queue3:" + queue3.Count());  
  18.         }  
  19.     }  

OUTPUT

pic1.jpg
 

Queue.Enqueue Method

 
Adds an object to the end of the Queue<T>. For example, 
  1. namespace Queue {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Queue < string > queue1 = newQueue < string > ();  
  5.             queue1.Enqueue("MCA");  
  6.             queue1.Enqueue("MBA");  
  7.             queue1.Enqueue("BCA");  
  8.             queue1.Enqueue("BBA");  
  9.             Console.WriteLine("The elements in the queue are:");  
  10.             foreach(string s in queue1) {  
  11.                 Console.WriteLine(s);  
  12.             }  
  13.         }  
  14.     }  

OUTPUT 

pic2.jpg
 
Queue.Dequeue() method
 
Removes and returns the object at the beginning of the Queue<T>. For example, 
  1. namespace Queue {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Queue < string > queue1 = newQueue < string > ();  
  5.             queue1.Enqueue("MCA");  
  6.             queue1.Enqueue("MBA");  
  7.             queue1.Enqueue("BCA");  
  8.             queue1.Enqueue("BBA");  
  9.             Console.WriteLine("The elements in the queue are:");  
  10.             foreach(string s in queue1) {  
  11.                 Console.WriteLine(s);  
  12.             }  
  13.             queue1.Dequeue(); //Removes the first element that enter in the queue here the first element is MCA  
  14.             queue1.Dequeue(); //Removes MBA  
  15.             Console.WriteLine("After removal the elements in the queue are:");  
  16.             foreach(string s in queue1) {  
  17.                 Console.WriteLine(s);  
  18.             }  
  19.         }  
  20.     }  
  21. }  
OUTPUT
 
pic3.jpg
 
Queue.Contain() method
 
Determines whether an element is in the Queue<T>. For example,
  1. namespace Queue {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Queue < string > queue1 = newQueue < string > ();  
  5.             queue1.Enqueue("MCA");  
  6.             queue1.Enqueue("MBA");  
  7.             queue1.Enqueue("BCA");  
  8.             queue1.Enqueue("BBA");  
  9.             Console.WriteLine("The elements in the queue are:");  
  10.             foreach(string s in queue1) {  
  11.                 Console.WriteLine(s);  
  12.             }  
  13.             Console.WriteLine("The element MCA is contain in the queue:" + queue1.Contains("MCA"));  
  14.             Console.WriteLine("The element BCA is contain in the queue:" + queue1.Contains("BCA"));  
  15.             Console.WriteLine("The element MTech is contain in the queue:" + queue1.Contains("MTech"));  
  16.         }  
  17.     }  

OUTPUT

pic4.jpg
 
Queue.Clear() method
 
Removes all objects from theQueue<T>. For example,
  1. namespace Queue {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Queue < string > queue1 = newQueue < string > ();  
  5.             queue1.Enqueue("MCA");  
  6.             queue1.Enqueue("MBA");  
  7.             queue1.Enqueue("BCA");  
  8.             queue1.Enqueue("BBA");  
  9.             Console.WriteLine("The elements in the queue are:" + queue1.Count());  
  10.             queue1.Clear();  
  11.             Console.WriteLine("The elements in the queue are after the clear method:" + queue1.Count());  
  12.         }  
  13.     }  

OUTPUT

pic5.jpg
 
Peek
 
Returns the object at the beginning of the Queue<T> without removing it. For example,
  1. namespace Queue {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Queue < string > queue1 = newQueue < string > ();  
  5.             queue1.Enqueue("MCA");  
  6.             queue1.Enqueue("MBA");  
  7.             queue1.Enqueue("BCA");  
  8.             queue1.Enqueue("BBA");  
  9.             Console.WriteLine("Peek the first item from the queue is:" + queue1.Peek());  
  10.             queue1.Dequeue();  
  11.             Console.WriteLine("Peek the next item from the queue is:" + queue1.Peek());  
  12.         }  
  13.     }  

OUTPUT

pic6.jpg
 
Queue.ToArray() method
 
Copies the Queue<T> elements to a new array. For example,
  1. namespace Queue {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Queue < string > queue1 = newQueue < string > ();  
  5.             queue1.Enqueue("MCA");  
  6.             queue1.Enqueue("MBA");  
  7.             queue1.Enqueue("BCA");  
  8.             queue1.Enqueue("BBA");  
  9.             Console.WriteLine("The queue elements are:");  
  10.             foreach(string s in queue1) {  
  11.                 Console.WriteLine(s);  
  12.             }  
  13.             Queue < string > queue2 = newQueue < string > (queue1.ToArray());  
  14.             Console.WriteLine("\nContents of the copy");  
  15.             foreach(string n in queue2) {  
  16.                 Console.WriteLine(n);  
  17.             }  
  18.         }  
  19.     }  

OUTPUT

pic7.jpg 
 

Summary

 
In this article I explained the Queue<> class and the various methods and properties of it.