English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Queue (Queue)

Queue is a special type of collection that stores elements in a FIFO (First In, First Out) manner, which is completely opposite to the Stack <T> collection. It contains elements in the order they are added. C# includes both generic Queue<T> and non-generic Queue collections. It is recommended to use the generic Queue<T> collection.

Queue <T> features

  • Queue<T> is a FIFO (First In, First Out) collection.  

  • It is under the System.Collection.Generic namespace.

  • Queue<T> can contain elements of a specified type. It provides compile-time type checking and does not perform boxing/unboxing because it is generic.

  • Elements can be added using the Enqueue() method. It cannot be used with collection initialization(collection-initializer syntax.

  • Elements can be retrieved using the Dequeue() and Peek() methods. It does not support indexers.

The following diagram illustrates the Queue collection:

Create a queue

You can create an object by specifying the type parameter for the element type stored in Queue<T>. The following example uses the Enqueue() method to create and add elements to Queue<T>. The Queue collection allows null (for reference types) and duplicate values.

Queue<int> callerIds = new Queue<int>();
callerIds.Enqueue(1);
callerIds.Enqueue(2);
callerIds.Enqueue(3);
callerIds.Enqueue(4);
foreach(var id in callerIds)
    Console.Write(id); //Print1234

Queue <T> properties and methods

PropertyUsage
CountReturns the total number of elements in the queue.
MethodUsage
Enqueue(T)Add an item to the queue.
DequeueReturns an item from the beginning of the queue and removes it from the queue.
Peek(T)Returns the first item from the queue without removing it.
Contains(T)Check if an item is in the queue
Clear()Remove all items from the queue.

Retrieve elements from the queue

The Dequeue() and Peek() methods are used to retrieve the first element in the queue collection. Dequeue() removes and returns the first element from a queue because the elements are stored in a FIFO (first in, first out) order. Calling the Dequeue() method on an empty queue will throw an InvalidOperation exception. Therefore, always check if the total number of the queue is greater than zero before calling the queue.

Queue<string> strQ = new Queue<string>();
strQ.Enqueue("H");
strQ.Enqueue("e");
strQ.Enqueue("l");
strQ.Enqueue("l");
strQ.Enqueue("o");
Console.WriteLine("Total number of elements: {0}", strQ.Count); //Output 5
while(strQ.Count > 0){
    Console.WriteLine(strQ.Dequeue()); //Output Hello
}
Console.WriteLine("Total number of elements: {0}", strQ.Count); //Output 0

The Peek() method always returns the first item from the queue collection without removing it. Calling this method on an empty queue will throw a runtime exception InvalidOperationException.

Queue<string> strQ = new Queue<string>();
strQ.Enqueue("H");
strQ.Enqueue("e");
strQ.Enqueue("l");
strQ.Enqueue("l");
strQ.Enqueue("o");
Console.WriteLine("Total number of elements: {0}", strQ.Count); //Output 5
if(strQ.Count > 0){
    Console.WriteLine(strQ.Peek()); //Output H
    Console.WriteLine(strQ.Peek()); //Output H
}
Console.WriteLine("Total number of elements: {0}", strQ.Count); //Output 0

Contains()

The Contains() method checks if an item exists in the queue. If the specified item exists, it returns true; otherwise, it returns false.

Contains() Signature:

 bool Contains(object obj);
Queue<int> callerIds = new Queue<int>();
callerIds.Enqueue(1);
callerIds.Enqueue(2);
callerIds.Enqueue(3);
callerIds.Enqueue(4);
callerIds.Contains(2); //true
callerIds.Contains(10); //false