C# Queue Peek

Description

Queue Peek returns the object at the beginning of the Queue without removing it.

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.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack