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 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:
// 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); } } } |
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:
// 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); } } } |
G E E K S
Reference:
Recommended Posts:
- C# | Copying the entire ArrayList to 1-D Array starting at the specified index
- Copying the Queue elements to 1-D Array in C#
- C# | Copying BitArray elements to an Array
- C# | Copying the Collection<T> elements to an array
- C# | Copying the SortedList elements to an Array Object
- C# | Copying the Hashtable elements to an Array Instance
- C# | Copying the HybridDictionary entries to an Array Instance
- C# | Copy the elements of collection over a range of elements in ArrayList
- C# | Get or set the number of elements that the ArrayList can contain
- C# | ArrayList whose elements are copies of the specified value
- C# | Sort the elements in the ArrayList
- C# | Remove all elements from the ArrayList
- C# | Adding the elements to the end of the ArrayList
- C# | Adding elements to the end of the ArrayList
- C# | Get the number of elements actually contained in the ArrayList
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.

