Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# | Copying the elements of ArrayList to a new array

  • Last Updated : 04 Aug, 2021

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:
 

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: 
 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!