Getting an object at the beginning of the Queue in C# (original) (raw)
Last Updated : 28 Jan, 2019
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.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.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:
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.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.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.Enumerator GetEnumerator (); Below programs illustrate the use of the above-disc 2 min read
- C# | Get object at the top of the Stack - Peek operation Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack.Peek Method is used to returns the object a 3 min read
- Finding the Index of First Element of the Specified Sequence in C# The Index Structure is introduced in C# 8.0. It represents a type that can be used to index a collection or sequence and it can be started from the start or the end. You are allowed to find the index, which points the first element of the specified collection or sequence with the help of Start Prope 2 min read
- Creating an Index From the Specified Index at the Start of a Collection in C# The Index Structure is introduced in C# 8.0. It represents a type that can be used to index a collection or sequence and it can be started from the start or the end. You are allowed to create a start index with the help of the FromStart(Int32) Method() provided by the Index struct. This method retur 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
- Copying the Queue elements to 1-D Array in C# Queue.CopyTo(T[], Int32) Method is used to copy the Queue elements to an existing one-dimensional Array, starting at the specified array index. The elements are copied to the Array in the same order in which the enumerator iterates through the Queue and this method is an O(n) operation, whe 4 min read