Convert arraylist to string array

In this article, we will learn how to convert ArrayList to string array. The ToArray() method copies the elements of the ArrayList to a new string array.

In the code given below, we create and initialize the ArrayList. Using ToArray() method we copy the elements of the ArrayList to a string array and display the array.



public void ConvertArrayListToarray()
        {
            ArrayList myArrayList = new ArrayList();
            myArrayList.Add("Jhon");
            myArrayList.Add("Jill");
            myArrayList.Add("Jo");
            myArrayList.Add("Chris");

            String[] myArray = (String[])myArrayList.ToArray(typeof(string));
            string str = string.Empty;
            for (int i = 0; i < myArray.Length; i++)
                           str += myArray[i] + "\n";
         }