Export (0) Print
Expand All

Queue.Peek Method

Returns the object at the beginning of the Queue without removing it.

[Visual Basic]
Public Overridable Function Peek() As Object
[C#]
public virtual object Peek();
[C++]
public: virtual Object* Peek();
[JScript]
public function Peek() : Object;

Return Value

The object at the beginning of the Queue.

Exceptions

Exception Type Condition
InvalidOperationException The Queue is empty.

Remarks

This method is similar to the Dequeue method, but Peek does not modify the Queue.

A null reference (Nothing in Visual Basic) can be added to the Queue as a value. To distinguish between a null value and the end of the Queue, check the Count property or catch the InvalidOperationException, which is thrown when the Queue is empty.

Example

[Visual Basic, C#, C++] The following example shows how to add elements to the Queue, remove elements from the Queue or just view the element at the beginning of the Queue.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesQueue
    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("The")
        myQ.Enqueue("quick")
        myQ.Enqueue("brown")
        myQ.Enqueue("fox")
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ, ControlChars.Tab)
        
        ' Removes an element from the Queue.
        Console.WriteLine("(Dequeue)" + ControlChars.Tab + "{0}", myQ.Dequeue())
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ, ControlChars.Tab)
        
        ' Removes another element from the Queue.
        Console.WriteLine("(Dequeue)" + ControlChars.Tab + "{0}", myQ.Dequeue())
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ, ControlChars.Tab)
        
        ' Views the first element in the Queue but does not remove it.
        Console.WriteLine("(Peek)   " + ControlChars.Tab + "{0}", myQ.Peek())
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ, ControlChars.Tab)
    End Sub
    
    Public Shared Sub PrintValues(myCollection As IEnumerable, _
       mySeparator As Char)
       
        Dim myEnumerator As System.Collections.IEnumerator = _
           myCollection.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Queue values:    The    quick    brown    fox
' (Dequeue)    The
' Queue values:    quick    brown    fox
' (Dequeue)    quick
' Queue values:    brown    fox
' (Peek)       brown
' Queue values:    brown    fox 

[C#] 
using System;
using System.Collections;
public class SamplesQueue  {

   public static void Main()  {

      // Creates and initializes a new Queue.
      Queue myQ = new Queue();
      myQ.Enqueue( "The" );
      myQ.Enqueue( "quick" );
      myQ.Enqueue( "brown" );
      myQ.Enqueue( "fox" );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ, '\t' );

      // Removes an element from the Queue.
      Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ, '\t' );

      // Removes another element from the Queue.
      Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ, '\t' );

      // Views the first element in the Queue but does not remove it.
      Console.WriteLine( "(Peek)   \t{0}", myQ.Peek() );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ, '\t' );
   }


   public static void PrintValues( IEnumerable myCollection, char mySeparator )  {
      System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Queue values:    The    quick    brown    fox
(Dequeue)    The
Queue values:    quick    brown    fox
(Dequeue)    quick
Queue values:    brown    fox
(Peek)       brown
Queue values:    brown    fox
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void PrintValues( IEnumerable* myCollection, Char mySeparator )  {
   System::Collections::IEnumerator* myEnumerator = myCollection->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::Write( "{0}{1}", __box(mySeparator), myEnumerator->Current );
   Console::WriteLine();
}

int main()  {
   // Creates and initializes a new Queue.
   Queue* myQ = new Queue();
   myQ->Enqueue( S"The" );
   myQ->Enqueue( S"quick" );
   myQ->Enqueue( S"brown" );
   myQ->Enqueue( S"fox" );

   // Displays the Queue.
   Console::Write( S"Queue values:" );
   PrintValues( myQ, '\t' );

   // Removes an element from the Queue.
   Console::WriteLine( S"(Dequeue)\t{0}", myQ->Dequeue() );

   // Displays the Queue.
   Console::Write( S"Queue values:" );
   PrintValues( myQ, '\t' );

   // Removes another element from the Queue.
   Console::WriteLine( S"(Dequeue)\t{0}", myQ->Dequeue() );

   // Displays the Queue.
   Console::Write( S"Queue values:" );
   PrintValues( myQ, '\t' );

   // Views the first element in the Queue but does not remove it.
   Console::WriteLine( S"(Peek)   \t{0}", myQ->Peek() );

   // Displays the Queue.
   Console::Write( S"Queue values:" );
   PrintValues( myQ, '\t' );
}

/*
This code produces the following output.

Queue values:   The     quick   brown   fox
(Dequeue)       The
Queue values:   quick   brown   fox
(Dequeue)       quick
Queue values:   brown   fox
(Peek)          brown
Queue values:   brown   fox
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Queue Class | Queue Members | System.Collections Namespace | Enqueue | Dequeue

Show:
© 2014 Microsoft