The
Dequeue() method is used to returns the object at the beginning of the Queue. This method is similar to the Peek() Method. The only difference between Dequeue and Peek method is that Peek() method will not modify the Queue but Dequeue will modify. This method is an O(1) operation and comes under
System.Collections.Generic namespace.
Syntax:
public T Dequeue ();
Return value: It returns the object which is removed from the beginning of the Queue.
Exception: The method throws
InvalidOperationException on calling empty queue, therefore always check that the total count of a queue is greater than zero before calling the Dequeue() method.
Below programs illustrate the use of the above-discussed method:
Output:
Number of elements in the Queue: 4
Top element of queue is:
3
Number of elements in the Queue: 3
Example 2: