Convert Queue To array in C#

CsharpServer Side ProgrammingProgramming

To convert queue to the array, the code is as follows −

Example

 Live Demo

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);
      }
   }
}

Output

This will produce the following output −

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

Example

Let us now see another example −

 Live Demo

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("Array...");
      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);
      }
   }
}

Output

This will produce the following output −

Array...
A
B
C
D
E
F
Convert Queue to Array...
A
B
C
D
E
F
raja
Published on 06-Dec-2019 11:12:26
Advertisements