Queue in C#

Queue in C# with Examples

In this article, I am going to discuss the Queue in C# with examples. Please read our previous article before proceeding to this article where we discussed the non-generic collection Stack in C# with some examples. 

What is a Queue in C#?

The Queue in C# is a non-generic collection class which works in the FIFO First In First Out) principle. So, we need to use the Queue Collection in C#, when we need the first in first out access to the items of a collection. That means the item which is added first will be removed first from the collection. When we add an item into the queue collection, it is called as enqueuing an item. Similarly when we remove an item from the queue collection then it is called as dequeuing an item. The Queue class belongs to the System.Collections namespace.

Let us understand the FIFO principle with an example. Imagine a queue of people waiting for the ticket in a cinema hall. Normally, the first person who enters the queue will be the first person to get the ticket from the counter. Similarly, the last person who enters into the queue will be the last person to get the ticket from the counter.

Note: The non-generic Queue Collection class in C# allows both null and duplicate values.

Methods of Queue class in C#:

Enqueue(): This method is used to add an item (or object) to the end of the Queue.
Syntax: void Queue.Enqueue(object obj)

Dequeue(): The Dequeue() method of the Queue class is used to Remove and return the object from the beginning of the Stack. If there is no object (or element) present in the Queue and if we are trying to remove an item or object from the Queue using the pop() method then it will throw an exception i.e. System.InvalidOperationException
Syntax: object Queue.Dequeue()

Peep(): The peep() method of the Queue class is used to return the oldest object i.e. the object present at the start of the Stack without removing it. If there is no object (or element) present in the Queue and if we are trying to return an item (object) from the Queue using the peep() method then it will throw an exception i.e. System.InvalidOperationException

Syntax: object Queue.Peep()

Example: Let us understand the above methods of Queue class with an example.
using System;
using System.Collections;

namespace QueueCollectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating a queue collection
            Queue q = new Queue();

            //Adding item to the stack using the push method
            q.Enqueue(10);
            q.Enqueue("hello");
            q.Enqueue(3.14f);
            q.Enqueue(true);
            q.Enqueue(67.8);
            q.Enqueue('A');

            //Printing the queue items using foreach loop
            foreach (object obj in q)
            {
                Console.Write(obj + " ");
            }
            Console.WriteLine();

            //Removing annd returning an item from the queue
            //using the Dequeue method
            Console.WriteLine(q.Dequeue());
            Console.WriteLine();

            //Printing item after removing the first added item
            foreach (object obj in q)
            {
                Console.Write(obj + " ");
            }
            Console.WriteLine();

            //Returning the first item from the queue without removing it
            //by using the peek method
            Console.WriteLine(q.Peek());
            Console.WriteLine();

            //Printing the items after using the Peek method
            foreach (object obj in q)
            {
                Console.Write(obj + " ");
            }
            Console.ReadKey();
        }
    }
}

OUTPUT:

Queue in C#

Let us discuss some other important methods and properties of Queue Class:

Count: The Count property of the Queue class is used to return the number of elements present in the Queue Collection.
Syntax: Queue.Count

Contains(): The Contains() method of the Queue class is used to Check whether an object (element) is present in the Queue or not. If it presents, then it will return true else it will return false.
Syntax: Queue.Contains(element)

Clear(): The Clear() method of the Queue class is used to remove all the elements from the queue collection.
Syntax: Queue.Clear()

Example: Let us see an example for a better understanding of the above methods and properties of Queue Class
using System;
using System.Collections;

namespace QueueCollectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating a queue collection
            Queue q = new Queue();

            //Adding item to the stack using the push method
            q.Enqueue(10);
            q.Enqueue("hi");
            q.Enqueue(3.14f);
            q.Enqueue(true);
            q.Enqueue(67.8);
            q.Enqueue('A');

            //Printing the queue items using foreach loop
            foreach (object obj in q)
            {
                Console.Write(obj + " ");
            }
            Console.WriteLine();

            //Using Count property to get the number of items
            //present in the queue collection
            Console.WriteLine($"No of Elements Present in the Collection : {q.Count}");
            Console.WriteLine();

            //Using the Contains method to check whether an item is present or not
            Console.WriteLine($"Is the value hi present in the collection : {q.Contains("hi")}");
            Console.WriteLine();

            //Removing all the items from the collection using Clear() method
            q.Clear();
            Console.WriteLine($"No of Elements in the Collection after Clear() method : {q.Count}");

            Console.ReadKey();
        }
    }
}

Output:

Queue in C#

In the next article, I am going to discuss the advantages and disadvantage of non-generic collection classes in C# with examples.

SUMMARY:

In this article, I try to explain the Queue in C# with some examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *