C# | Copying the elements of ArrayList to a new array Last Updated : 04 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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 Array.Copy, which is an O(n) operation, where n is Count.Syntax: public virtual object[] ToArray (); Return Value: This method will return an Object array containing copies of the elements of the ArrayList.Example: CSharp // C# program to illustrate ToArray() Method using System; using System.Collections; class GFG { // Main Method public static void Main() { // Create and initializing ArrayList ArrayList mylist = new ArrayList(5); mylist.Add("G"); mylist.Add("E"); mylist.Add("E"); mylist.Add("K"); mylist.Add("S"); // Copy the data of Arraylist into // the object Array Using ToArray() // method object[] str2 = mylist.ToArray(); foreach(string i in str2) { Console.WriteLine(i); } } } Output: G E E K S ToArray(Type) This method is used to copy the elements of the ArrayList to a new array of the specified element type. The elements are copied using Array.Copy, which is an O(n) operation, where n is Count.Syntax: public virtual Array ToArray (Type t); Here, t is the element Type of the destination array to create and copy elements to. Return Value : This method will return an array of the specified element type containing copies of the elements of the ArrayList.Exception: If the value of t is null then this method will give ArgumentNullException.If the type of the source ArrayList cannot be cast automatically to the specified type, then this method will give InvalidCastException. Note: All of the objects in the ArrayList object will be cast to the Type specified in the type parameter.Example: CSharp // C# program to illustrate ToArray(Type) Method using System; using System.Collections; class GFG { // Main Method public static void Main() { // Create and initialize new array ArrayList mylist = new ArrayList(5); mylist.Add("G"); mylist.Add("E"); mylist.Add("E"); mylist.Add("K"); mylist.Add("S"); // Copy the data of Arraylist into // the string Array Using // ToArray(Type) method string[] str2 = (string[])mylist.ToArray(typeof(string)); // Display the data of str2 string foreach(string i in str2) { Console.WriteLine(i); } } } Output: G E E K S Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.toarray?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Copying the elements of ArrayList to a new array A ankita_saini Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-ArrayList Similar Reads 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 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# | 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 C# | Copying the SortedList elements to an Array Object SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destinat 2 min read C# | Copying the Collection<T> elements to an array Collection<T>.CopyTo(T[], Int32) method is used to copy the entire Collection<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 : The one-dimensional Array that is the destin 3 min read Like