# Queue.Peek Método

## Definição

Retorna o objeto ao início da Queue sem removê-lo.Returns the object at the beginning of the Queue without removing it.

``````public:
virtual System::Object ^ Peek();``````
``public virtual object Peek ();``
``public virtual object? Peek ();``
``````abstract member Peek : unit -> obj
override this.Peek : unit -> obj``````
``Public Overridable Function Peek () As Object``

#### Retornos

Object

O objeto no início da Queue.The object at the beginning of the Queue.

#### Exceções

O Queue está vazio.The Queue is empty.

## Exemplos

O exemplo a seguir mostra como adicionar elementos ao Queue , remover elementos do Queue ou exibir o elemento no início do Queue .The following example shows how to add elements to the Queue, remove elements from the Queue, or view the element at the beginning of the Queue.

``````using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection );
int main()
{

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

// Displays the Queue.
Console::Write( "Queue values:" );
PrintValues( myQ );

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

// Displays the Queue.
Console::Write( "Queue values:" );
PrintValues( myQ );

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

// Displays the Queue.
Console::Write( "Queue values:" );
PrintValues( myQ );

// 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 );
}

void PrintValues( IEnumerable^ myCollection )
{
IEnumerator^ myEnum = myCollection->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::Write( "    {0}", obj );
}

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

*/
``````
``````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 );

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

// Displays the Queue.
Console.Write( "Queue values:" );
PrintValues( myQ );

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

// Displays the Queue.
Console.Write( "Queue values:" );
PrintValues( myQ );

// 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 );
}

public static void PrintValues( IEnumerable myCollection )  {
foreach ( Object obj in myCollection )
Console.Write( "    {0}", obj );
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

*/
``````
``````Imports System.Collections

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)

' Removes an element from the Queue.
Console.WriteLine("(Dequeue)    {0}", myQ.Dequeue())

' Displays the Queue.
Console.Write("Queue values:")
PrintValues(myQ)

' Removes another element from the Queue.
Console.WriteLine("(Dequeue)    {0}", myQ.Dequeue())

' Displays the Queue.
Console.Write("Queue values:")
PrintValues(myQ)

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

' Displays the Queue.
Console.Write("Queue values:")
PrintValues(myQ)
End Sub

Public Shared Sub PrintValues(myCollection As IEnumerable)
Dim obj As [Object]
For Each obj In  myCollection
Console.Write("    {0}", obj)
Next obj
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

``````

## Comentários

Esse método é semelhante ao Dequeue método, mas não Peek modifica o Queue .This method is similar to the Dequeue method, but Peek does not modify the Queue.

`null` pode ser adicionado ao Queue como um valor.`null` can be added to the Queue as a value. Para distinguir entre um valor nulo e o final do Queue , verifique a Count propriedade ou Capture o InvalidOperationException , que é gerado quando o Queue está vazio.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.

Esse método é uma `O(1)` operação.This method is an `O(1)` operation.