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)
- Difference between Method Overriding and Method Hiding in C#
- Extension Method in C#
- Anonymous Method in C#
- C# | Uri.IsHexEncoding() Method
- C# | ToLower() Method
- DateTimeOffset.Add() Method in C#
- C# | Method Overriding
- C# | PadRight() Method
- C# | EndsWith() Method
- C# | Trim() Method
- C# | CopyTo() Method
- C# | ToUpper() Method
- C# | Copy() Method
- TimeSpan.Add() Method in C#
- C# | IsNullOrEmpty() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.