Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Queue.Peek Method in C#
The Queue.Peek() method in C# is used to return the object at the beginning of the Queue without removing it. This method is useful when you need to examine the first element in the queue without modifying the queue's contents.
Syntax
Following is the syntax for the non-generic Queue −
public virtual object Peek();
Following is the syntax for the generic Queue<T> −
public T Peek();
Return Value
The method returns the object at the beginning of the Queue. For generic queues, it returns type T, and for non-generic queues, it returns object.
Using Queue.Peek() with Non-Generic Queue
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
Queue queue = new Queue();
queue.Enqueue("AB");
queue.Enqueue("BC");
queue.Enqueue("CD");
queue.Enqueue("DE");
queue.Enqueue("EF");
Console.WriteLine("Queue contents:");
IEnumerator demoEnum = queue.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine(demoEnum.Current);
}
Console.WriteLine("Queue element at the beginning = " + queue.Peek());
Console.WriteLine("Queue count after Peek = " + queue.Count);
}
}
The output of the above code is −
Queue contents: AB BC CD DE EF Queue element at the beginning = AB Queue count after Peek = 5
Using Queue.Peek() with Generic Queue
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<string> queue = new Queue<string>();
queue.Enqueue("Gary");
queue.Enqueue("Jack");
queue.Enqueue("Ryan");
queue.Enqueue("Kevin");
queue.Enqueue("Mark");
Console.WriteLine("Count of elements = " + queue.Count);
Console.WriteLine("Queue element at the beginning = " + queue.Peek());
Console.WriteLine("Does the queue have element Jack? = " + queue.Contains("Jack"));
// Peek again to show element is still there
Console.WriteLine("Peek again = " + queue.Peek());
Console.WriteLine("Count remains = " + queue.Count);
}
}
The output of the above code is −
Count of elements = 5 Queue element at the beginning = Gary Does the queue have element Jack? = True Peek again = Gary Count remains = 5
Peek vs Dequeue Comparison
| Queue.Peek() | Queue.Dequeue() |
|---|---|
| Returns the first element without removing it | Returns and removes the first element |
| Queue count remains unchanged | Queue count decreases by 1 |
| Can be called multiple times with same result | Each call returns the next element |
| Throws exception if queue is empty | Throws exception if queue is empty |
Conclusion
The Queue.Peek() method provides a safe way to examine the first element in a queue without modifying its contents. It's particularly useful for checking what element will be processed next without actually processing it, making it ideal for scenarios where you need to conditionally dequeue elements.
