C# | How to copy the entire ArrayList to a one-dimensional Array Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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. The Array must have zero-based indexing. Exceptions: 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: Example 1: CSharp // C# code to illustrate the // ArrayList.CopyTo 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"); myList.Add("E"); myList.Add("F"); myList.Add("G"); myList.Add("H"); // Creates and initializes the // one-dimensional target Array. String[] arr = new String[9]; arr[0] = "C"; arr[1] = "C++"; arr[2] = "Java"; arr[3] = "Python"; arr[4] = "C#"; arr[5] = "HTML"; arr[6] = "CSS"; arr[7] = "PHP"; arr[8] = "DBMS"; Console.WriteLine("Before CopyTo Method: "); Console.WriteLine("\nArrayList Contains: "); // printing ArrayList elements foreach(Object obj in myList) Console.WriteLine("{0}", obj); Console.WriteLine("\nString Array Contains: "); // printing String elements foreach(Object obj1 in arr) Console.WriteLine("{0}", obj1); Console.WriteLine("After CopyTo Method: "); // using CopyTo Method to copy // the entire source ArrayList // to the target Array starting // at index 0 myList.CopyTo(arr); Console.WriteLine("\nArrayList Contains: "); // printing ArrayList elements foreach(Object obj in myList) Console.WriteLine("{0}", obj); Console.WriteLine("\nString Array Contains: "); // printing String elements foreach(Object obj1 in arr) Console.WriteLine("{0}", obj1); } } Output: 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 Example 2: CSharp // C# code to illustrate the // ArrayList.CopyTo 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("HTML"); myList.Add("CSS"); myList.Add("PHP"); myList.Add("DBMS"); // 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 CopyTo Method: "); Console.WriteLine("\nArrayList Contains: "); // printing ArrayList elements foreach(Object obj in myList) Console.WriteLine("{0}", obj); Console.WriteLine("\nString Array Contains: "); // printing String elements foreach(Object obj1 in arr) Console.WriteLine("{0}", obj1); Console.WriteLine("After CopyTo Method: "); // using CopyTo 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); Console.WriteLine("\nArrayList Contains: "); // printing ArrayList elements foreach(Object obj in myList) Console.WriteLine("{0}", obj); Console.WriteLine("\nString Array Contains: "); // printing String elements foreach(Object obj1 in arr) Console.WriteLine("{0}", obj1); } } 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_ Comment More infoAdvertise with us Next Article C# | How to copy the entire ArrayList to a one-dimensional Array K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-ArrayList Similar Reads 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# | How to convert an ArrayList to Array In C#, an array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float etc. and the elements are stored in a contiguous location.ArrayList represen 4 min read How to create a shallow copy of ArrayList in C# ArrayList.Clone() Method is used to create a shallow copy of the specified ArrayList. A shallow copy of a collection copies only the elements of the collection irrespective of reference types or value types. But it does not copy the objects that the references refer to. The references in the new col 3 min read C# | Copying BitArray elements to an Array The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.CopyTo(Array, Int32) method is used to copy the ent 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 Like