Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Queue.CopyTo() Method in C#
The Queue.CopyTo() method in C# is used to copy the Queue elements to an existing one-dimensional Array, starting at the specified array index. This method is particularly useful when you need to transfer queue elements into an array while preserving the FIFO (First In, First Out) order.
Syntax
Following is the syntax for the Queue.CopyTo() method −
public virtual void CopyTo(Array arr, int index);
Parameters
-
arr − The one-dimensional Array that is the destination of the elements copied from Queue. The Array must have zero-based indexing.
-
index − The zero-based index in the array at which copying begins.
Return Value
This method does not return a value. It performs an in-place copy operation on the destination array.
Using CopyTo() with Integer Queue
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);
Console.Write("Count of elements = ");
Console.WriteLine(queue.Count);
Console.WriteLine("Queue...");
foreach(int i in queue) {
Console.WriteLine(i);
}
int[] intArr = new int[5];
intArr[0] = 1;
intArr[1] = 2;
intArr[2] = 3;
intArr[3] = 4;
queue.CopyTo(intArr, 1);
Console.WriteLine("\nQueue (After CopyTo)");
foreach(int i in queue) {
Console.WriteLine(i);
}
Console.WriteLine("\nArray (After CopyTo)");
foreach(int i in intArr) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Count of elements = 3 Queue... 100 200 300 Queue (After CopyTo) 100 200 300 Array (After CopyTo) 1 100 200 300 0
Using CopyTo() with String Queue
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<string> queue = new Queue<string>();
queue.Enqueue("Tim");
queue.Enqueue("Jack");
queue.Enqueue("Nathan");
queue.Enqueue("Tom");
queue.Enqueue("David");
queue.Enqueue("Mark");
Console.Write("Count of elements = ");
Console.WriteLine(queue.Count);
Console.WriteLine("Queue...");
foreach(string i in queue) {
Console.WriteLine(i);
}
string[] strArr = new string[10];
strArr[0] = "AB";
strArr[1] = "BC";
strArr[2] = "DE";
strArr[3] = "EF";
queue.CopyTo(strArr, 1);
Console.WriteLine("\nQueue (After CopyTo)");
foreach(string i in queue) {
Console.WriteLine(i);
}
Console.WriteLine("\nArray (After CopyTo)");
foreach(string i in strArr) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Count of elements = 6 Queue... Tim Jack Nathan Tom David Mark Queue (After CopyTo) Tim Jack Nathan Tom David Mark Array (After CopyTo) AB Tim Jack Nathan Tom David Mark
Key Points
-
The
CopyTo()method does not modify the original queue − it remains unchanged after the copy operation. -
The destination array must have sufficient space from the specified index to accommodate all queue elements.
-
Elements are copied in the same order they would be dequeued (FIFO order).
-
If the destination array is too small, an
ArgumentExceptionwill be thrown.
Conclusion
The Queue.CopyTo() method provides an efficient way to transfer queue elements into an existing array while preserving their FIFO order. The original queue remains unchanged, making it useful for creating snapshots or backups of queue data for further processing.
