Create a Queue from another collection in C#?

Creating a Queue from another collection in C# can be accomplished using the Queue constructor that accepts an IEnumerable<T> parameter. This allows you to initialize a new Queue with elements from arrays, lists, other queues, or any collection that implements IEnumerable<T>.

Syntax

Following is the syntax to create a Queue from another collection −

Queue<T> newQueue = new Queue<T>(sourceCollection);

Where sourceCollection can be any collection implementing IEnumerable<T> such as arrays, lists, or other queues.

Using Queue Constructor with Array

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        // Create an array
        string[] colors = {"Red", "Blue", "Green", "Yellow"};
        
        // Create a Queue from the array
        Queue<string> colorQueue = new Queue<string>(colors);
        
        Console.WriteLine("Queue created from array:");
        foreach(string color in colorQueue) {
            Console.WriteLine(color);
        }
    }
}

The output of the above code is −

Queue created from array:
Red
Blue
Green
Yellow

Using Queue Constructor with List

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        // Create a List
        List<int> numbers = new List<int>() {10, 20, 30, 40, 50};
        
        // Create a Queue from the List
        Queue<int> numberQueue = new Queue<int>(numbers);
        
        Console.WriteLine("Queue created from List:");
        foreach(int number in numberQueue) {
            Console.WriteLine(number);
        }
        
        Console.WriteLine("\nDequeue operations:");
        while(numberQueue.Count > 0) {
            Console.WriteLine("Dequeued: " + numberQueue.Dequeue());
        }
    }
}

The output of the above code is −

Queue created from List:
10
20
30
40
50

Dequeue operations:
Dequeued: 10
Dequeued: 20
Dequeued: 30
Dequeued: 40
Dequeued: 50

Using ToArray() Method to Copy Queue

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        // Create original queue
        Queue<string> originalQueue = new Queue<string>();
        originalQueue.Enqueue("First");
        originalQueue.Enqueue("Second");
        originalQueue.Enqueue("Third");
        
        Console.WriteLine("Original Queue:");
        foreach(string str in originalQueue) {
            Console.WriteLine(str);
        }
        
        // Create new queue from original using ToArray()
        Queue<string> copiedQueue = new Queue<string>(originalQueue.ToArray());
        
        Console.WriteLine("\nCopied Queue:");
        foreach(string str in copiedQueue) {
            Console.WriteLine(str);
        }
        
        // Verify they are independent
        originalQueue.Dequeue();
        Console.WriteLine("\nAfter dequeue from original:");
        Console.WriteLine("Original count: " + originalQueue.Count);
        Console.WriteLine("Copied count: " + copiedQueue.Count);
    }
}

The output of the above code is −

Original Queue:
First
Second
Third

Copied Queue:
First
Second
Third

After dequeue from original:
Original count: 2
Copied count: 3

Common Use Cases

  • Converting arrays to queues for FIFO processing of existing data

  • Copying queues to preserve original data while performing operations

  • Converting lists to queues when you need queue-specific operations like Enqueue/Dequeue

  • Processing collections in order where first-in, first-out behavior is required

Conclusion

Creating a Queue from another collection in C# is straightforward using the Queue constructor that accepts any IEnumerable<T>. This approach preserves the original order of elements and creates an independent copy, making it useful for converting between collection types while maintaining FIFO behavior.

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

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements