Home  |  Delphi .net Home  |  System.Collections.Queue  |  Peek Method
Peek  
Method  
Peek at, but do not DeQueue the begining item on the current Queue
Queue Class
System.Collections NameSpace
CF1.  Function Peek ( ) : Object;
CF : Methods with this mark are Compact Framework Compatible
Description
The value of the current item at the beginning of the current Queue is returned, but not removed from the Queue. Use of DeQueue returns the value and removes the item from teh Queue.
Microsoft MSDN Links
System.Collections
System.Collections.Queue
 
 
Illustrating how Peek does not update the Queue
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  MyQueue    : System.Collections.Queue;
  Enumerator : IEnumerator;

begin
  // Create our queue
  MyQueue := Queue.Create;

  // Add entries to the queue
  MyQueue.EnQueue('First');
  MyQueue.EnQueue('Second');
  MyQueue.Enqueue('Third');

  // Display the queue
  Console.WriteLine('Queue contains :');
  Console.WriteLine;
  Enumerator := MyQueue.GetEnumerator;
  while enumerator.MoveNext do
    Console.WriteLine(Enumerator.Current.ToString);

  // Show the next item to be DeQueued
  Console.WriteLine;
  Console.WriteLine('Peeking at the next item to be DeQueued :');
  Console.WriteLine;

  Console.WriteLine(MyQueue.Peek.ToString);

  Console.WriteLine;
  Console.WriteLine('Show that the Queue is unaffacted :');
  Console.WriteLine;
  Enumerator := MyQueue.GetEnumerator;
  while enumerator.MoveNext do
    Console.WriteLine(Enumerator.Current.ToString);

  Console.Readline;
end.
Show full unit code
  Queue contains :
  
  First
  Second
  Third
  
  Peeking at the next item to be DeQueued :
  
  First
  
  Show that the Queue is unaffacted :
  
  First
  Second
  Third
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author