C# | Copying the entire ArrayList to 1-D Array starting at the specified index Last Updated : 23 Jan, 2019 Comments Improve Suggest changes Like Article Like Report ArrayList.CopyTo(Array, Int32) Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. arrayIndex: It is the zero-based index in array at which copying begins. Exceptions: ArgumentNullException: If the array is null. ArgumentOutOfRangeException: If the arrayIndex is less than zero. 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: Example 1: CSharp // C# code to illustrate the // ArrayList.CopyTo(Array, Int32) // Method using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating an ArrayList ArrayList myList = new ArrayList(); // Adding elements to ArrayList myList.Add("A"); myList.Add("B"); myList.Add("C"); myList.Add("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("\nArrayList Contains: "); // Displaying the elements in myList for (int i = 0; i < myList.Count; i++) { Console.WriteLine("myList[{0}] : {1}", i, myList[i]); } 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 ArrayList // to the target Array starting at // index 2. myList.CopyTo(arr, 2); Console.WriteLine("\nArrayList Contains: "); // Displaying the elements in myList for (int i = 0; i < myList.Count; i++) { Console.WriteLine("myList[{0}] : {1}", i, myList[i]); } 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: ArrayList Contains: myList[0] : A myList[1] : B myList[2] : C myList[3] : D Array Contains: arr[0] : HTML arr[1] : PHP arr[2] : Java arr[3] : Python arr[4] : C# arr[5] : OS After Method: ArrayList Contains: myList[0] : A myList[1] : B myList[2] : C myList[3] : 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 // ArrayList.CopyTo(Array, Int32) // Method using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating an ArrayList ArrayList myList = new ArrayList(); // Adding elements to ArrayList myList.Add("A"); myList.Add("B"); myList.Add("C"); myList.Add("D"); // Creates and initializes the // one-dimensional target Array. // Here array size is only 2 i.e // it can hold only 3 elements. String[] arr = new String[2]; Console.WriteLine("Before Method: "); Console.WriteLine("\nArrayList Contains: "); // Displaying the elements in myList for (int i = 0; i < myList.Count; i++) { Console.WriteLine("myList[{0}] : {1}", i, myList[i]); } 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 ArrayList is greater // than the number of elements that // the destination array can contain myList.CopyTo(arr, 2); Console.WriteLine("\nArrayList Contains: "); // Displaying the elements in myList for (int i = 0; i < myList.Count; i++) { Console.WriteLine("myList[{0}] : {1}", i, myList[i]); } 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.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds Parameter name: destinationArray Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.copyto?view=netframework-4.7.2#System_Collections_ArrayList_CopyTo_System_Array_System_Int32_ Comment More infoAdvertise with us Next Article C# | Copying the entire ArrayList to 1-D Array starting at the specified index K Kirti_Mangal Follow Improve Article Tags : C# C# Programs CSharp-method CSharp-Collections-Namespace CSharp-Collections-ArrayList +1 More Similar Reads C# | Copy StringDictionary to Array at the specified index StringDictionary.CopyTo(Array, Int32) method is used to copy the string dictionary values to a one-dimensional Array instance at the specified index. Syntax: public virtual void CopyTo (Array array, int index); Parameters: array: It is the one-dimensional Array which is the destination of the values 3 min read C# | Copying the elements of ArrayList to a new array ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows: ToArray()ToArray(Type)ToArray() This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Arra 2 min read C# | Copy StringCollection at the specified index of array StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.CopyTo(String[], Int32) method is used to copy the entire StringCollection values 3 min read C# | How to copy the entire ArrayList to a one-dimensional Array ArrayList.CopyTo Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. Syntax: public virtual void CopyTo (Array array); Here, array is the one-dimensional Array which is the destination of the elements copied from ArrayList 3 min read C# | Remove the element at the specified index of the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.RemoveAt(Int32) method is used to remove the element at the speci 3 min read C# | Insert an element into the ArrayList at the specified index ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayLis 3 min read Copying the Queue elements to 1-D Array in C# Queue<T>.CopyTo(T[], Int32) 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, whe 4 min read C# | Copy ListDictionary to Array instance at the specified index ListDictionary.CopyTo(Array, Int32) method is used to copy the ListDictionary entries to a one-dimensional Array instance at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array : It is the one-dimensional Array which is the destination of the DictionaryEntry o 3 min read C# | Copying the HybridDictionary entries to an Array Instance HybridDictionary.CopyTo(Array, Int32) method is used to copy the HybridDictionary entries to a one-dimensional Array instance at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry obje 3 min read C# | Copy the entire LinkedList<T> to Array LinkedList<T>.CopyTo(T[], Int32) method is used to copy the entire LinkedList<T> to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public void CopyTo (T[] array, int index); Parameters: array : It is the one-dimensional Array that is the 2 min read Like