Tag: .Net

Generic Queue of C#

Hi Friends,

In the below post we will learn about the Generic Queue (Queue<T>) of C#.

What is Queue?

In the real world example, you can visualize queue with a line of people waiting to get a movie ticket at ticket counter. The person who come first gets ticket first and other peoples join the line from last. we can say that this follows the first in first out (FIFO) rule.

The C# provide you a generic implementation of queue(Queue<T>) under in System.Collections.Generic namespace.

How to declare object of Queue<T>() ?

You can define a object of specified type using below syntax

Queue<T> instanceName = new Queue<T>();

Here I am defining a integer queue as below.

Queue<int> studentsNoQueue = new Queue<int>();

The framework gives you two more constructors to define queue object one takes capacity as input and other takes initial elements of enumerable collection as input.

How to add an element to Queue?

To Add an element to a Queue you can use the ‘Enqueue(T)’ method which add element at the last of the queue.An example is as below.

studentsNoQueue.Enqueue(12);
studentsNoQueue.Enqueue(14);
studentsNoQueue.Enqueue(16);
studentsNoQueue.Enqueue(20);

How to remove an element from Queue?
To Remove an element from the Queue object you can use the ‘Dequeue()’ method which returns the element which are top in the queue.

studentsNoQueue.Dequeue()

How to get an element without removing it from Queue?

To get an element( accordingly FIFO rule) from queue without removing, you can use ‘Peek()’ method.

studentsNoQueue.Peek()

You can also loop through the queue.
How to clear a Queue Object?
For this you can user ‘Clear()’ method.

 studentsNoQueue.Clear()

A sample program is as below.

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace GenericsSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Queue studentsNoQueue = new Queue();
                studentsNoQueue.Enqueue(12);
                studentsNoQueue.Enqueue(14);
                studentsNoQueue.Enqueue(16);
                studentsNoQueue.Enqueue(20);

                Debug.WriteLine("\nInitial Queue");
                foreach (var item in studentsNoQueue)
                {
                    Debug.Write(item + "  ");
                }
                Debug.WriteLine("\n Using Dequeue()");
                Debug.WriteLine(studentsNoQueue.Dequeue() + "  ");
                Debug.WriteLine("\n Using Peek()");
                Debug.WriteLine(studentsNoQueue.Peek() + "  ");
                Debug.WriteLine("\nAfter  Peek()");
                foreach (var item in studentsNoQueue)
                {
                    Debug.Write(item + "  ");
                }
                studentsNoQueue.Clear();
            }
            catch(InvalidOperationException ex)
            {
                Debug.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            Console.ReadLine();
          
        }
    }
}

Hope this helps you.:)