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 virtual Object Peek()

Returns

Queue.Peek method returns The object at the beginning of the Queue.

Example

The following example shows how to add elements to the Queue, view the element at the beginning of the Queue.


using System;//from   www .  j a  va2 s  .  c om
using System.Collections;
public class SamplesQueue  {

   public static void Main()  {
      Queue myQ = new Queue();
      myQ.Enqueue( "A" );
      myQ.Enqueue( "B" );
      myQ.Enqueue( "C" );
      myQ.Enqueue( "D" );

      foreach ( Object obj in myQ ){
         Console.WriteLine( "    {0}", obj );
      }

      Console.WriteLine( "(Peek)   \t{0}", myQ.Peek() );
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack