HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Queue Collection Type
A queue class is like a queue at a movie theatre. A person who comes into the queue first leaves first. As people keep coming they get added at the end of the queue.

The Queue class is a first-in-first-out(FIFO) collection class that is present in System.Collections namespace. Use Queue if you need to access the information in the same order that it is stored in the collection.
Example
In the example below
  • Enqueue() method adds an element to the end of the Queue.
  • Dequeue() method removes the oldest element from the start of the Queue.
  • Peek() method returns the oldest element from the start of the Queue but does not remove it from the Queue.
  • Count property determines the number of elements contained in the Queue.
  • Clear() method removes all the items from the Queue.

using System;

using System.Collections;

class QueueDemo

{

    public static void Main()

    {

        //Create an instance of Queue

        Queue Q = new Queue();

        //Add elements to the Queue using Enqueue() Method

        Q.Enqueue("Prasad");

        Q.Enqueue("Giri");

        Q.Enqueue("Ravi");

        //Dequeue() removes and returns the object at the begining of the queue

        Console.WriteLine(Q.Dequeue());

        //Count determines the number of elements contained in the Queue

        //Count will be 2 as we removed the first object from the queue

        Console.WriteLine("Count = " + Q.Count);

        //Peek() method returns the object at the begining of the queue without

        //removing from the queue

        Console.WriteLine(Q.Peek());

        //The count will still be 2 as the peek() method will not remove the object

        //from the queue

        Console.WriteLine("Count = " + Q.Count);

        //Remove all the items from the Queue using Clear() method

        Q.Clear();

    }

}

Other related articles

Njoy Programming
   ByPrasad Cherukuri