Queue.CopyTo() Method in C# Last Updated : 04 Feb, 2019 Comments Improve Suggest changes Like Article Like Report This method is used to copy the Queue elements to an existing one-dimensional Array, starting at the specified array index. The elements are copied to the Array in the same order in which the enumerator iterates through the Queue and this method is an O(n) operation, where n is Count. This method comes under System.Collections namespace. Syntax: public virtual void CopyTo (Array array, int index); Parameters: array: It is the one-dimensional Array that is the destination of the elements copied from Queue. The Array must have zero-based indexing. index: It is the zero-based index in array at which copying begins. Exceptions: ArgumentNullException: If the array is null. ArgumentOutOfRangeException: If the index is less than zero. ArgumentException: If the array is multidimensional Or the number of elements in the source Queue is greater than the number of elements that the destination array can contain. InvalidCastException: If the type of the source Queue cannot be cast automatically to the type of the destination array. Below programs illustrate the use of above-discussed method: Example 1: csharp // C# code to illustrate the // Queue.CopyTo(Array, Int32) // Method using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating an Queue Queue myq = new Queue(); // Adding elements to Queue myq.Enqueue("A"); myq.Enqueue("B"); myq.Enqueue("C"); myq.Enqueue("D"); // Creates and initializes the // one-dimensional target Array. String[] arr = new String[6]; // adding elements to Array arr[0] = "HTML"; arr[1] = "PHP"; arr[2] = "Java"; arr[3] = "Python"; arr[4] = "C#"; arr[5] = "OS"; Console.WriteLine("Before Method: "); Console.WriteLine("\nQueue Contains: "); // Displaying the elements in myq foreach(Object obj in myq) { Console.WriteLine(obj); } Console.WriteLine("\nArray Contains: "); // Displaying the elements in arr for (int i = 0; i < arr.Length; i++) { Console.WriteLine("arr[{0}] : {1}", i, arr[i]); } Console.WriteLine("After Method: "); // Copying the entire source Queue // to the target Array starting at // index 2. myq.CopyTo(arr, 2); Console.WriteLine("\nQueue Contains: "); // Displaying the elements in myq foreach(Object obj in myq) { Console.WriteLine(obj); } Console.WriteLine("\nArray Contains: "); // Displaying the elements in arr for (int i = 0; i < arr.Length; i++) { Console.WriteLine("arr[{0}] : {1}", i, arr[i]); } } } Output: Before Method: Queue Contains: A B C D Array Contains: arr[0] : HTML arr[1] : PHP arr[2] : Java arr[3] : Python arr[4] : C# arr[5] : OS After Method: Queue Contains: A B C D Array Contains: arr[0] : HTML arr[1] : PHP arr[2] : A arr[3] : B arr[4] : C arr[5] : D Example 2: csharp // C# code to illustrate the // Queue.CopyTo(Array, Int32) // Method using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating an Queue Queue myq = new Queue(); // Adding elements to Queue myq.Enqueue("A"); myq.Enqueue("B"); myq.Enqueue("C"); myq.Enqueue("D"); // Creates and initializes the // one-dimensional target Array. String[] arr = new String[2]; // adding elements to Array arr[0] = "HTML"; arr[1] = "PHP"; arr[2] = "Java"; arr[3] = "Python"; arr[4] = "C#"; arr[5] = "OS"; Console.WriteLine("Before Method: "); Console.WriteLine("\nQueue Contains: "); // Displaying the elements in myq foreach(Object obj in myq) { Console.WriteLine(obj); } Console.WriteLine("\nArray Contains: "); // Displaying the elements in arr for (int i = 0; i < arr.Length; i++) { Console.WriteLine("arr[{0}] : {1}", i, arr[i]); } Console.WriteLine("After Method: "); // using Method but It will give // Runtime Error as number of elements // in the source Queue is greater // than the number of elements that // the destination array can contain myq.CopyTo(arr, 2); Console.WriteLine("\nQueue Contains: "); // Displaying the elements in myq foreach(Object obj in myq) { Console.WriteLine(obj); } Console.WriteLine("\nArray Contains: "); // Displaying the elements in arr for (int i = 0; i < arr.Length; i++) { Console.WriteLine("arr[{0}] : {1}", i, arr[i]); } } } Runtime Error: Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at (wrapper stelemref) System.Object.virt_stelemref_sealed_class(intptr, object) Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.queue.copyto?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article Queue.Dequeue Method in C# K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-Queue Similar Reads C# Queue Class In C#, the Queue<T> class is the part of the System.Collections.Generic namespace and represent a first-in-first-out (FIFO) collection of objects. When we add an item to the list, it is called enqueue, and when we remove an item, it is called dequeue.Enqueue adds an element to the end of the Q 5 min read How to create a Queue in C# Queue() Constructor is used to initializes a new instance of the Queue class which will be empty, and will have the default initial capacity, and uses the default growth factor. Queue represents a first-in, first out collection of object. It is used when you need first-in, first-out access of items. 2 min read Queue.Count Property in C# This property is used to get the number of elements contained in the Queue. Retrieving the value of this property is an O(1) operation and it comes under the System.Collections namespace. Syntax: public virtual int Count { get; } Property Value: This property returns the number of elements contained 2 min read Queue.IsSynchronized Property in C# This property is used get a value which indicates whether access to the Queue is synchronized (thread safe) or not. Syntax: public virtual bool IsSynchronized { get; } Property Value: This property returns true if access to the Queue is synchronized(thread safe) otherwise, false. The default is fals 2 min read How to get Synchronize access to the Queue in C# Queue.SyncRoot Property is used to get an object which can be used to synchronize access to the Queue. Queue represents a first-in, first out collection of object. It is used when you need first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remov 2 min read Queue.Clear Method in C# This method is used to remove the objects from the Queue. This method is an O(n) operation, where n is the total count of elements. And this method comes under System.Collections namespace. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: 2 min read Queue.Clone() Method in C# This method is used to create a shallow copy of the Queue. It just creates a copy of the Queue. The copy will have a reference to a clone of the internal elements but not a reference to the original elements. Syntax: public virtual object Clone (); Return Value: The method returns an Object which is 2 min read Queue.Contains() Method in C# This method is used to check whether an element is in the Queue. This method performs a linear search, therefore, this method is an O(n) operation, where n is Count. And this method comes under the System.Collections namespace. Syntax: public virtual bool Contains(object obj); Here, obj is the Objec 2 min read Queue.CopyTo() Method in C# This method is used to copy the Queue elements to an existing one-dimensional Array, starting at the specified array index. The elements are copied to the Array in the same order in which the enumerator iterates through the Queue and this method is an O(n) operation, where n is Count. This method co 4 min read Queue.Dequeue Method in C# The Dequeue() method is used to returns the object at the beginning of the Queue. This method is similar to the Peek() Method. The only difference between Dequeue and Peek method is that Peek() method will not modify the Queue but Dequeue will modify. This method is an O(1) operation and comes under 2 min read Like