C# Queue

Queue Collections come under System.Collection.Generic namespace. It stores in the FIFO form i.e. First In First Out, unlike Stack which stores in the LIFO form. Enqueue is when data is inserted in the queue, whereas Dequeue is when it is removed.

Create a Queue and Enqueue

Add elements in the Queue with the Enqueue() method in C#. Enqueue means inserting elements to the end of the queue. To create a Queue in C#, with string elements, use <string>. Set the type in angular braces as shown below:

Queue<string> queue = new Queue<string>();

Let us see an example and create a queue with some string elements:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a queue
       Queue<string> queue = new Queue<string>();

       // Insert 5 elements to the Queue
       queue.Enqueue("Amphibians");
       queue.Enqueue("Reptiles");
       queue.Enqueue("Mammals");
       queue.Enqueue("Fish");
       queue.Enqueue("Birds");

       // Display the elements
       Console.WriteLine("Display the queue elements...\n");
       foreach (string s in queue)
       {
           Console.WriteLine(s);
       }  
    }
  }
}

Output

Display the queue elements...

Amphibians
Reptiles
Mammals
Fish
Birds

Dequeue

To return the first element (beginning of the queue) from the queue and remove it, use the Dequeue() method in C#. Let us see an example:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a queue
       Queue<string> queue = new Queue<string>();

       // Insert 5 elements to the Queue
       queue.Enqueue("Amphibians");
       queue.Enqueue("Reptiles");
       queue.Enqueue("Mammals");
       queue.Enqueue("Fish");
       queue.Enqueue("Birds");

       // Display the elements
       Console.WriteLine("Display the queue elements...");
       foreach (string s in queue)
       {
          Console.WriteLine(s);
       }  
       
      // Removes the first element from the Queue
      queue.Dequeue(); 
      
      // Display the updated elements
      Console.WriteLine("\nDisplay the updated queue elements...");
      foreach (string s in queue)
      {
         Console.WriteLine(s);
      }  
    }
  }
}

Output

Display the queue elements...
Amphibians
Reptiles
Mammals
Fish
Birds

Display the updated queue elements...
Reptiles
Mammals
Fish
Birds

Count the number of elements

The Count property is used to count the number of elements in the Queue. Let us see an example:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a queue
       Queue<string> queue = new Queue<string>();

       // Insert 5 elements to the Queue
       queue.Enqueue("Amphibians");
       queue.Enqueue("Reptiles");
       queue.Enqueue("Mammals");
       queue.Enqueue("Fish");
       queue.Enqueue("Birds");

       // Display the elements
       Console.WriteLine("Display the queue elements...");
       foreach (string s in queue)
       {
          Console.WriteLine(s);
       }  
      
      // Display the count of elements
      Console.WriteLine("\nNumber of elements = "+queue.Count);    
    }
  }
}

Output

Display the queue elements...
Amphibians
Reptiles
Mammals
Fish
Birds

Number of elements = 5

Check for an element in the queue

The Contains() method is used to check whether an element exists in the queue or not. Let us see an example:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a queue
       Queue<string> queue = new Queue<string>();

       // Insert 5 elements to the Queue
       queue.Enqueue("Amphibians");
       queue.Enqueue("Reptiles");
       queue.Enqueue("Mammals");
       queue.Enqueue("Fish");
       queue.Enqueue("Birds");

       // Display the elements
       Console.WriteLine("Display the queue elements...");
       foreach (string s in queue)
       {
          Console.WriteLine(s);
       }  
      
      // Check for an element in the Queue
      Console.WriteLine("\nAre Mammals in the Queue? = "+queue.Contains("Mammals"));
    }
  }
}

Output

Display the queue elements...
Amphibians
Reptiles
Mammals
Fish
Birds

Are Mammals in the Queue? = True

Peek an element

To return the first element from the top of the queue, without removing it, use the Peek() method. Let us see an example:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a queue
       Queue<string> queue = new Queue<string>();

       // Insert 5 elements to the Queue
       queue.Enqueue("Amphibians");
       queue.Enqueue("Reptiles");
       queue.Enqueue("Mammals");
       queue.Enqueue("Fish");
       queue.Enqueue("Birds");

       // Display the elements
       Console.WriteLine("Display the queue elements...");
       foreach (string s in queue)
       {
          Console.WriteLine(s);
       }  
      
      // Get the first element from the queue
      Console.WriteLine("\n1st element = "+queue.Peek());   
    }
  }
}

Output

Display the queue elements...
Amphibians
Reptiles
Mammals
Fish
Birds

1st element = Amphibians

Read More

C# Stack
C# vs Java - Difference
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment