Return the object at the beginning of the Queue without removing it in CSharp

Description

The following code shows how to return the object at the beginning of the Queue without removing it.

Example


using System;/* w  ww  .  ja v  a  2  s  .  co  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 »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary