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
public virtual void CopyTo (Array array, int index);
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.
- ArgumentNullException: If the array is null.
- ArgumentOutOfRangeException: If the index is less than zero.
- ArgumentException: If the array is multidimensional
Orthe 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:
Before Method: Queue Contains: A B C D Array Contains: arr : HTML arr : PHP arr : Java arr : Python arr : C# arr : OS After Method: Queue Contains: A B C D Array Contains: arr : HTML arr : PHP arr : A arr : B arr : C arr : D
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at (wrapper stelemref) System.Object.virt_stelemref_sealed_class(intptr, object)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.