ArrayList.CopyTo Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
public virtual void CopyTo (Array array);
Here, array is the one-dimensional Array which is the destination of the elements copied from ArrayList. The Array must have zero-based indexing.
- ArgumentNullException: If the array is null.
- ArgumentException: If the array is multidimensional OR the number of elements in the source ArrayList is greater than the number of elements that the destination array can contain.
- InvalidCastException: If the type of the source ArrayList cannot be cast automatically to the type of the destination array.
Below programs illustrate the use of above-discussed method:
Before CopyTo Method: ArrayList Contains: A B C D E F G H String Array Contains: C C++ Java Python C# HTML CSS PHP DBMS After CopyTo Method: ArrayList Contains: A B C D E F G H String Array Contains: A B C D E F G H DBMS
System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array’s lower bounds
Parameter name: destinationArray
- C# | Copying the entire ArrayList to 1-D Array starting at the specified index
- C# | Reverse the order of the elements in the entire ArrayList or in the specified range
- C# | Getting an enumerator for the entire ArrayList
- C# | Copy the entire LinkedList<T> to Array
- C# | Copy the elements of collection over a range of elements in ArrayList
- How to create a shallow copy of ArrayList in C#
- Shallow Copy and Deep Copy in C#
- C# | Reverse the order of the elements in the entire List or in the specified range
- C# | Copying the elements of ArrayList to a new array
- C# | Array vs ArrayList
- C# | How to convert an ArrayList to Array
- C# | Remove the first occurrence of a specific object from the ArrayList
- C# | Get the number of elements actually contained in the ArrayList
- C# | Check whether an element is contained in the ArrayList
- C# | Check if the ArrayList is read-only
- C# | Insert an element into the ArrayList at the specified index
- C# | Add an object to the end of the ArrayList
- C# | Get or set the number of elements that the ArrayList can contain
- C# | Remove a range of elements from the ArrayList
- C# | Check if the ArrayList has a fixed size
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.