Queue.Enqueue() Method in C#

The Queue.Enqueue() method in C# is used to add an object to the end of the Queue. This method follows the FIFO (First In, First Out) principle where elements are added at the rear and removed from the front.

Syntax

For generic Queue<T> collections −

public void Enqueue(T item);

For non-generic Queue collections −

public virtual void Enqueue(object obj);

Parameters

item/obj − The object to add to the end of the Queue. The value can be null for reference types.

Queue.Enqueue() Operation A B C D Front Rear Enqueue New elements added at the rear

Using Queue.Enqueue() with String Elements

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(){
        Queue<string> queue = new Queue<string>();
        
        // Adding elements to the queue
        queue.Enqueue("Gary");
        queue.Enqueue("Jack");
        queue.Enqueue("Ryan");
        queue.Enqueue("Kevin");
        queue.Enqueue("Mark");
        
        Console.WriteLine("Initial Queue:");
        foreach(string item in queue){
            Console.WriteLine(item);
        }
        Console.WriteLine("Count: " + queue.Count);
        
        // Adding more elements
        queue.Enqueue("Lisa");
        queue.Enqueue("Tom");
        
        Console.WriteLine("\nAfter adding more elements:");
        foreach(string item in queue){
            Console.WriteLine(item);
        }
        Console.WriteLine("Count: " + queue.Count);
    }
}

The output of the above code is −

Initial Queue:
Gary
Jack
Ryan
Kevin
Mark
Count: 5

After adding more elements:
Gary
Jack
Ryan
Kevin
Mark
Lisa
Tom
Count: 7

Using Queue.Enqueue() with Different Data Types

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(){
        Queue<int> numberQueue = new Queue<int>();
        
        // Adding integers to the queue
        numberQueue.Enqueue(10);
        numberQueue.Enqueue(20);
        numberQueue.Enqueue(30);
        
        Console.WriteLine("Integer Queue:");
        foreach(int num in numberQueue){
            Console.WriteLine(num);
        }
        
        // Demonstrating FIFO behavior
        Console.WriteLine("\nRemoving elements (FIFO):");
        while(numberQueue.Count > 0){
            Console.WriteLine("Dequeued: " + numberQueue.Dequeue());
            Console.WriteLine("Remaining count: " + numberQueue.Count);
        }
    }
}

The output of the above code is −

Integer Queue:
10
20
30

Removing elements (FIFO):
Dequeued: 10
Remaining count: 2
Dequeued: 20
Remaining count: 1
Dequeued: 30
Remaining count: 0

Combining Enqueue() with Other Queue Operations

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(){
        Queue<string> taskQueue = new Queue<string>();
        
        // Adding tasks to the queue
        taskQueue.Enqueue("Task 1");
        taskQueue.Enqueue("Task 2");
        taskQueue.Enqueue("Task 3");
        
        Console.WriteLine("Task Queue: " + taskQueue.Count + " tasks");
        
        // Peek at the front element without removing it
        Console.WriteLine("Next task: " + taskQueue.Peek());
        
        // Process tasks one by one
        Console.WriteLine("\nProcessing tasks:");
        while(taskQueue.Count > 0){
            string currentTask = taskQueue.Dequeue();
            Console.WriteLine("Processing: " + currentTask);
        }
        
        Console.WriteLine("\nAll tasks completed. Queue count: " + taskQueue.Count);
    }
}

The output of the above code is −

Task Queue: 3 tasks
Next task: Task 1

Processing tasks:
Processing: Task 1
Processing: Task 2
Processing: Task 3

All tasks completed. Queue count: 0

Conclusion

The Queue.Enqueue() method adds elements to the rear of the queue, maintaining FIFO order. It works with both generic and non-generic queue collections and is essential for implementing queue-based data processing scenarios like task scheduling and breadth-first algorithms.

Updated on: 2026-03-17T07:04:36+05:30

486 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements