Queue Class:-
Queue Class:-
  • It represents a first-in, first out collection of object.
  • It is used when you need a first-in, first-out access of items.
  • When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque.

Methods and Properties of the Queue Class:-

Methods of the Queue Class:-

Sr.No. Methods
1 public virtual void Clear();

Removes all elements from the Queue.

2 public virtual bool Contains(object obj);

Determines whether an element is in the Queue.

3 public virtual object Dequeue();

Removes and returns the object at the beginning of the Queue.

4 public virtual void Enqueue(object obj);

Adds an object to the end of the Queue.

5 public virtual object[] ToArray();

Copies the Queue to a new array.

6 public virtual void TrimToSize();

Sets the capacity to the actual number of elements in the Queue.


Properties of the Queue Class:-

Property Description
Count Gets the number of elements contained in the Queue.

Example of Queue:-

  

using System;
using System.Collections;

class QueueDemo
{
    public QueueDemo()
    {
        Console.WriteLine("This is queue demo::\n");
    }

    public void Show()
    {
        Queue q = new Queue();

        q.Enqueue('S');
        q.Enqueue('A');
        q.Enqueue('N');
        q.Enqueue('T');
        q.Enqueue('O');
        q.Enqueue('S');
        q.Enqueue('H');

        Console.WriteLine("Current queue: ");
        foreach (char c in q) Console.Write(c + " ");

        Console.WriteLine();
        q.Enqueue('H');
        q.Enqueue('S');
        Console.WriteLine("Current queue: ");
        foreach (char c in q) Console.Write(c + " ");

        Console.WriteLine();
        Console.WriteLine("Removing some values ");
        char ch = (char)q.Dequeue();
        Console.WriteLine("The removed value: {0}", ch);
        ch = (char)q.Dequeue();
        Console.WriteLine("The removed value: {0}", ch);

        Console.ReadKey();
    }
}

class Test
{
    static void Main(string[] args)
    {       
        QueueDemo objQe = new QueueDemo();
        objQe.Show();
        Console.ReadLine();
    }
}