Convert Queue To array in C#

Converting a queue to an array in C# is accomplished using the ToArray() method, which is available for the Queue<T> generic collection. This method creates a new array containing all elements from the queue in the same order they were added (FIFO - First In, First Out).

Syntax

Following is the syntax for converting a queue to an array −

T[] array = queue.ToArray();

Where T is the type of elements in the queue, and queue is the Queue<T> instance.

Return Value

The ToArray() method returns a new array of type T[] containing copies of all elements from the queue. The original queue remains unchanged after the conversion.

Converting Integer Queue to Array

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue<int> queue = new Queue<int>();
      queue.Enqueue(100);
      queue.Enqueue(200);
      queue.Enqueue(300);
      queue.Enqueue(400);
      queue.Enqueue(500);
      queue.Enqueue(600);
      queue.Enqueue(700);
      queue.Enqueue(800);
      queue.Enqueue(900);
      queue.Enqueue(1000);
      Console.WriteLine("Queue...");
      foreach(int i in queue) {
         Console.WriteLine(i);
      }
      int[] intArr = queue.ToArray();
      Console.WriteLine("Convert Queue to Array...");
      foreach(int i in intArr) {
         Console.WriteLine(i);
      }
   }
}

The output of the above code is −

Queue...
100
200
300
400
500
600
700
800
900
1000
Convert Queue to Array...
100
200
300
400
500
600
700
800
900
1000

Converting String Queue to Array

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue<string> queue = new Queue<string>();
      queue.Enqueue("A");
      queue.Enqueue("B");
      queue.Enqueue("C");
      queue.Enqueue("D");
      queue.Enqueue("E");
      queue.Enqueue("F");
      Console.WriteLine("Queue...");
      foreach(string i in queue) {
         Console.WriteLine(i);
      }
      string[] strArr = queue.ToArray();
      Console.WriteLine("Convert Queue to Array...");
      foreach(string i in strArr) {
         Console.WriteLine(i);
      }
   }
}

The output of the above code is −

Queue...
A
B
C
D
E
F
Convert Queue to Array...
A
B
C
D
E
F

Key Points

  • The ToArray() method preserves the FIFO order of elements when converting to an array.

  • The original queue remains unchanged after calling ToArray().

  • The method creates a shallow copy of the elements in a new array.

  • Works with any generic type Queue<T> where T can be any data type.

Conclusion

The ToArray() method provides a simple and efficient way to convert a Queue<T> to an array in C#. This conversion maintains the original FIFO order of elements and creates a new array without modifying the source queue, making it useful for scenarios where you need array-based operations on queue data.

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

396 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements