Getting an object at the beginning of the Queue in C# Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: csharp // C# Program to illustrate the use // of Queue<T>.Dequeue Method using System; using System.Collections.Generic; class GFG { // Main Method public static void Main() { // creating a queue of integers Queue<int> queue = new Queue<int>(); queue.Enqueue(3); queue.Enqueue(2); queue.Enqueue(1); queue.Enqueue(4); Console.WriteLine("Number of elements in the Queue: {0}", queue.Count); // Retrieveing top element of queue Console.WriteLine("Top element of queue is:"); Console.WriteLine(queue.Dequeue()); // printing the no of queue element // after dequeue operation Console.WriteLine("Number of elements in the Queue: {0}", queue.Count); } } Output: Number of elements in the Queue: 4 Top element of queue is: 3 Number of elements in the Queue: 3 Example 2: csharp // C# Program to illustrate the use // of Queue<T>.Dequeue Method using System; using System.Collections.Generic; class GFG { // Main Method public static void Main() { Queue<int> queue = new Queue<int>(); // Adding elements in Queue queue.Enqueue(2); queue.Enqueue(5); Console.WriteLine("Number of elements in the Queue: {0}", queue.Count); // Retrieveing top element of queue Console.WriteLine("Top element of queue is:"); Console.WriteLine(queue.Dequeue()); // printing the no. of queue element // after dequeue operation Console.WriteLine("Number of elements in the Queue: {0}", queue.Count); } } Output: Number of elements in the Queue: 2 Top element of queue is: 2 Number of elements in the Queue: 1 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.queue-1.dequeue?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check if an element is in the Queue K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Generic-Queue CSharp-Generic-Namespace Similar Reads C# | Get the object at the beginning of the Queue - Peek Operation Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.Peek Method is used to get the object at the beginning of the Qu 3 min read C# | Add an object to the end of the Queue - Enqueue Operation Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called dequeue. Queue<T>.Enqueue(T) Method is used to add an object to the end 3 min read C# | Get the number of elements contained in the Queue Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.Count Property is used to get the number of elements contained i 2 min read Getting enumerator that iterates through the Queue in C# Queue<T>.GetEnumerator Method is used to get an enumerator which can iterate through the Queue. And it comes under the System.Collections.Generic namespace. Syntax: public System.Collections.Generic.Queue<T>.Enumerator GetEnumerator (); Below programs illustrate the use of the above-disc 2 min read C# | Check if an element is in the Queue Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.Contains(T) Method is used to check whether an element is in the 2 min read C# | Remove all objects from the Queue Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue. Clear Method is used to remove the objects from the Queue. This 3 min read Like