10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Queue Collection in C#

Updated on Sep 3, 2025
 
45,996 Views

The Queue Collection is used when the data items need to be arranged in a FIFO (First In First Out) manner. When a data item is entered into the collection, it is called enqueue. When the data item is removed from the collection, it is called dequeue.

Constructors in Queue Collection

The different constructors and their description is given as follows:

Constructors

Description

Queue<T>()

This constructor initializes a new instance of the Queue<T> class that is empty and has the default initial capacity.

Queue<T>(IEnumerable<T>)

This constructor initializes a new instance of the Queue<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.

Queue<T>(Int32)

This constructor initializes a new instance of the Queue<T> class that is empty and has the specified initial capacity.

Table: Constructors in Queue Collection in C#

Properties in Queue Collection

The different properties and their description is given as follows:

Properties

Description

Count

This property gets the number of elements contained in the Queue<T>.

Table: Properties in Queue Collection in C#

Methods in Queue Collection

The different methods and their description is given as follows:

Methods

Description

Clear()

This method removes all objects from the Queue<T>.

Contains(T)

This method determines whether an element is in the Queue<T> or not.

CopyTo(T[], Int32)

This method copies the Queue<T> elements to an existing one-dimensional Array, starting at the specified array index.

Dequeue()

This method removes and returns the object at the beginning of the Queue<T>.

Enqueue(T)

This method adds an object to the end of the Queue<T>.

Equals(Object)

This method determines whether the specified object is equal to the current object.

GetEnumerator()

This method returns an enumerator that iterates through the Queue<T>.

GetHashCode()

This method serves as the default hash function.

GetType()

This method gets the Type of the current instance.

MemberwiseClone()

This method creates a shallow copy of the current Object.

Peek()

This method returns the object at the beginning of the Queue<T> without removing it.

ToArray()

This method copies the Queue<T> elements to a new array.

ToString()

This method returns a string that represents the current object.

TrimExcess()

This method sets the capacity to the actual number of elements in the Queue<T>, if that number is less than 90 percent of current capacity.

Table: Methods in Queue Collection in C#
Source: MSDN

Queue Operations

Some of the stack operations are given as follows:

Enqueue

An element is added to the end of the queue using the Enqueue() method. The program that demonstrates this is given as follows:

using System;
using System.Collections;
namespace QueueDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Queue q = new Queue();
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(3);
        q.Enqueue(4);
        Console.WriteLine("Queue elements are:");
        foreach (int i in q)
        {
            Console.Write(i + " ");
        }
     }
  }
}

Source Code: Program to enqueue elements in Queue in C#

The output of the above program is as follows:

Queue elements are:
1 2 3 4

Dequeue

The element at the beginning of the queue can be removed using the Dequeue() method. The program that demonstrates this is given as follows:

using System;
using System.Collections;
namespace QueueDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Queue q = new Queue();
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(3);
        q.Enqueue(4);
        Console.Write("Original queue: ");
        foreach (int i in q)
        {
            Console.Write(i + " ");
        }
        q.Dequeue();
        q.Dequeue();
        Console.WriteLine();
        Console.Write("Queue after Dequeue() operation: ");
        foreach (int i in q)
        {
            Console.Write(i + " ");
        }
     }
  }
}

Source Code: Program to dequeue elements in Queue in C#

The output of the above program is as follows:

Original queue: 1 2 3 4
Queue after Dequeue() operation: 3 4

Peek

The element at the beginning of the queue can be viewed without removing it using the Peek() method. The program that demonstrates this is given as follows:

using System;
using System.Collections;
namespace QueueDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Queue q = new Queue();
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(3);
        q.Enqueue(4);
        Console.Write("Queue elements are: ");
        foreach (int i in q)
        {
            Console.Write(i + " ");
        }
        Console.WriteLine();
        Console.Write("Element at the beginning of the queue is: {0}", q.Peek());
     }
  }
}

Source Code: Program to peek beginning element in Queue in C#

The output of the above program is as follows:

Queue elements are: 1 2 3 4
Element at the beginning of the queue is: 1

Count the number of elements in a Queue

To count the number of elements in a Queue, use the Count property:

using System;
using System.Collections;

namespace QueueExample {
  class Example {
     static void Main(string[] args) {
        Queue q = new Queue();
       
        q.Enqueue('A');
        q.Enqueue('B');
        q.Enqueue('C');
        q.Enqueue('D');
        q.Enqueue('E');
        q.Enqueue('F');
        q.Enqueue('G');
        q.Enqueue('H');
       
        Console.WriteLine("Count = {0} ", q.Count);
       
        Console.ReadKey();
     }
  }
}

Source Code: Program to count the number of elements in a Queue in C#

The output of the above program is as follows:

Count = 8
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses