C# Queue:

C# includes a Queue collection class in the System.Collection namespace. Queue stores the elements in FIFO style (First In First Out), exactly opposite of the Stack collection. It contains the elements in the order they were added.

Queue collection allows multiple null and duplicate values. Use the Enqueue() method to add values and the Dequeue() method to retrieve the values from the Queue.

C# queue

Important Properties and Methods of Queue:

Property Usage
Count Returns the total count of elements in the Queue.
Method Usage
Enqueue Adds an item into the queue.
Dequeue Removes and returns an item from the beginning of the queue.
Peek Returns an first item from the queue
Contains Checks whether an item is in the queue or not
Clear Removes all the items from the queue.
TrimToSize Sets the capacity of the queue to the actual number of items in the queue.

Add elements in Queue:

Queue is a non-generic collection. So you can add elements of any datatype into a queue using the Enqueue() method.

Enqueue() signature: void Enqueue(object obj)

Example: Enqueue()

Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Access Queue:

Dequeue() method is used to retrieve the top most element in a queue collection. Dequeue() removes and returns a first element from a queue because the queue stores elements in FIFO order. Calling Dequeue() method on empty queue will throw InvalidOperation exception. So always check that the total count of a queue is greater than zero before calling the Dequeue() method on a queue.

Dequeue() method signature: object Dequeue()

Example: Dequeue()

Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);

while (queue.Count > 0)
    Console.WriteLine(queue.Dequeue());

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);

Output:
Number of elements in the Queue: 4
3
2
1
Four
Number of elements in the Queue: 0

Peek():

The Peek() method always returns the first item from a queue collection without removing it from the queue. Calling Peek() and Dequeue() methods on an empty queue collection will throw a run time exception "InvalidOperationException".

Peek() Method Signature: object Peek();

Example: Peek()

Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);

Console.WriteLine(queue.Peek());
Console.WriteLine(queue.Peek());
Console.WriteLine(queue.Peek());

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);

Output:
Number of elements in the Queue: 4
3
3
3
Number of elements in the Queue: 4

You can iterate a Queue without removing elements of it by converting in to an Array, as below:

Example: Iterate Queue

Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Console.WriteLine("Number of elements in Queue: {0}", queue.Count);

foreach (var i in queue.ToArray())
        Console.WriteLine(i);

Console.WriteLine("Number of elements in Queue: {0}", queue.Count);

Output:
Number of elements in Queue: 4
3
2
1
Four
Number of elements in Queue: 4

Contains:

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

Contains() Signature: bool Contains(object obj);

Example: Contains

Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

queue.Contains(2); // true
queue.Contains(100); //false

Clear():

The Clear() method removes all the items from a queue.

Clear() Signature: void Clear();

Example: Clear()

Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);

queue.Clear();

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);

Output:
Number of elements in the Queue: 4
Number of elements in the Queue: 0

Points to Remember :

  1. The Queue stores the values in FIFO (First in First out) style. The element which is added first will come out First.
  2. Use the Enqueue() method to add elements into Queue
  3. The Dequeue() method returns and removes elements from the beginning of the Queue. Calling the Dequeue() method on an empty queue will throw an exception.
  4. The Peek() method always returns top most element.

Visit MSDN for more information on members of Queue.