Open In App

C# Queue with Examples

A Queue is used to represent a first-in, first out(FIFO) collection of objects. It is used when you need first-in, first-out access of items. It is the non-generic type of collection which is defined in System.Collections namespace. It is used to create a dynamic collection which grows, according to the need of your program. In Queue, you can store elements of the same type and of the different types. Generally, a queue is helpful when you access that information in the same way in which they stored in the collection and it is temporary storage to store data.



The below diagram illustrates the Queue class hierarchy:



Important Points:

How to create the Queue?

Queue class has four constructors which are used to create the queue which are as follows:

Let’s see how to create an Queue using Queue() constructor:

Step 1: Include System.Collections namespace in your program with the help of using keyword.

Syntax:

using System.Collections;

Step 2: Create an queue using Queue class as shown below:

Queue queue_name = new Queue();

Step 3: If you want to add elements in your queue then use Enqueue() method to add elements in your queue. As shown in the below example.

Example:




// C# program to illustrate queue
using System;
using System.Collections;
  
public class GFG {
    static public void Main()
    {
  
        // Create a queue
        // Using Queue class
        Queue my_queue = new Queue();
  
        // Adding elements in Queue
        // Using Enqueue() method
        my_queue.Enqueue("GFG");
        my_queue.Enqueue(1);
        my_queue.Enqueue(100);
        my_queue.Enqueue(null);
        my_queue.Enqueue(2.4);
        my_queue.Enqueue("Geeks123");
  
        // Accessing the elements
        // of my_queue Queue
        // Using foreach loop
        foreach(var ele in my_queue)
        {
            Console.WriteLine(ele);
        }
    }
}

Output:
GFG
1
100

2.4
Geeks123

How to remove elements from the Queue?

In Queue, you are allowed to remove elements from the queue. The Queue class provides two different methods to remove elements and the methods are:

How to get topmost element of the queue?

In Queue, you can easily find the topmost element of the queue by using the following methods provided by the Queue class:

How to check the availability of elements in the queue?

In Queue, you can check whether the given element is present or not using Contain() method. Or in other words, if you want to search an element in the given queue use Contains() method.

Example:




// C# program to illustrate how
// to check element present in
// the queue or not
using System;
using System.Collections;
  
class GFG {
  
    static public void Main()
    {
  
        // Create a queue
        // Using Queue class
        Queue my_queue = new Queue();
  
        // Adding elements in Queue
        // Using Enqueue() method
        my_queue.Enqueue("GFG");
        my_queue.Enqueue("Geeks");
        my_queue.Enqueue("GeeksforGeeks");
        my_queue.Enqueue("geeks");
        my_queue.Enqueue("Geeks123");
  
        // Checking if the element is
        // present in the Queue or not
        if (my_queue.Contains("GeeksforGeeks") == true) {
            Console.WriteLine("Element available...!!");
        }
        else {
            Console.WriteLine("Element not available...!!");
        }
    }
}

Output:
Element available...!!

Generic Queue Vs Non-Generic Queue

Generic Queue Non-Generic Queue
Generic queue is defined under System.Collections.Generic namespace. Non-Generic queue is defined under System.Collections namespace.
Generic queue can only store same type of elements. Non-Generic queue can store same type or different types of elements.
There is a need to define the type of the elements in the queue. There is no need to define the type of the elements in the queue.
It is type- safe. It is not type-safe.

Article Tags :
C#