C# Queue Peek
Description
Queue
returns the object at the beginning
of the Queue
Syntax
Queue.Peek
has the following syntax.
public T Peek()
Returns
Queue.Peek
method returns The object at the beginning of the Queue.
Example
using System;//from ww w. jav a 2s. c o m
using System.Collections.Generic;
class Example
{
public static void Main()
{
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
foreach( string number in numbers )
{
Console.WriteLine(number);
}
Console.WriteLine(numbers.Dequeue());
Console.WriteLine(numbers.Peek());
Console.WriteLine(numbers.Dequeue());
}
}
The code above generates the following result.