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:

  1. ToArray()
  2. 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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate ToArray() Method
using System;
using System.Collections;
  
class GFG {
      
    // Main Method
    public static void Main()
    {
          
        // Create and initalizing 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);
        }
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate ToArray(Type) Method
using System;
using System.Collections;
  
class GFG {
      
    // Main Method
    public static void Main()
    {
          
        // Create and initalize 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);
        }
    }
}

chevron_right


Output:

G
E
E
K
S

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.