C# | Copying the entire ArrayList to 1-D Array starting at the specified index
ArrayList.CopyTo(Array, Int32) Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
Syntax:
public virtual void CopyTo (Array array, int arrayIndex);
Parameters:
array: It is the one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing.
arrayIndex: It is the zero-based index in array at which copying begins.
Exceptions:
- ArgumentNullException: If the array is null.
- ArgumentOutOfRangeException: If the arrayIndex is less than zero.
- ArgumentException: If the array is multidimensional OR the number of elements in the source ArrayList is greater than the number of elements that the destination array can contain.
- InvalidCastException: If the type of the source ArrayList cannot be cast automatically to the type of the destination array.
Below programs illustrate the use of above-discussed method:
Example 1:
// C# code to illustrate the // ArrayList.CopyTo(Array, Int32) // Method using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating an ArrayList ArrayList myList = new ArrayList(); // Adding elements to ArrayList myList.Add("A"); myList.Add("B"); myList.Add("C"); myList.Add("D"); // Creates and initializes the // one-dimensional target Array. String[] arr = new String[6]; // adding elements to Array arr[0] = "HTML"; arr[1] = "PHP"; arr[2] = "Java"; arr[3] = "Python"; arr[4] = "C#"; arr[5] = "OS"; Console.WriteLine("Before Method: "); Console.WriteLine("\nArrayList Contains: "); // Displaying the elements in myList for (int i = 0; i < myList.Count; i++) { Console.WriteLine("myList[{0}] : {1}", i, myList[i]); } Console.WriteLine("\nArray Contains: "); // Displaying the elements in arr for (int i = 0; i < arr.Length; i++) { Console.WriteLine("arr[{0}] : {1}", i, arr[i]); } Console.WriteLine("After Method: "); // Copying the entire source ArrayList // to the target Array starting at // index 2. myList.CopyTo(arr, 2); Console.WriteLine("\nArrayList Contains: "); // Displaying the elements in myList for (int i = 0; i < myList.Count; i++) { Console.WriteLine("myList[{0}] : {1}", i, myList[i]); } Console.WriteLine("\nArray Contains: "); // Displaying the elements in arr for (int i = 0; i < arr.Length; i++) { Console.WriteLine("arr[{0}] : {1}", i, arr[i]); } } } |
Output:
Before Method: ArrayList Contains: myList[0] : A myList[1] : B myList[2] : C myList[3] : D Array Contains: arr[0] : HTML arr[1] : PHP arr[2] : Java arr[3] : Python arr[4] : C# arr[5] : OS After Method: ArrayList Contains: myList[0] : A myList[1] : B myList[2] : C myList[3] : D Array Contains: arr[0] : HTML arr[1] : PHP arr[2] : A arr[3] : B arr[4] : C arr[5] : D
Example 2:
// C# code to illustrate the // ArrayList.CopyTo(Array, Int32) // Method using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating an ArrayList ArrayList myList = new ArrayList(); // Adding elements to ArrayList myList.Add("A"); myList.Add("B"); myList.Add("C"); myList.Add("D"); // Creates and initializes the // one-dimensional target Array. // Here array size is only 2 i.e // it can hold only 3 elements. String[] arr = new String[2]; Console.WriteLine("Before Method: "); Console.WriteLine("\nArrayList Contains: "); // Displaying the elements in myList for (int i = 0; i < myList.Count; i++) { Console.WriteLine("myList[{0}] : {1}", i, myList[i]); } Console.WriteLine("\nArray Contains: "); // Displaying the elements in arr for (int i = 0; i < arr.Length; i++) { Console.WriteLine("arr[{0}] : {1}", i, arr[i]); } Console.WriteLine("After Method: "); // using Method but It will give // Runtime Error as number of elements // in the source ArrayList is greater // than the number of elements that // the destination array can contain myList.CopyTo(arr, 2); Console.WriteLine("\nArrayList Contains: "); // Displaying the elements in myList for (int i = 0; i < myList.Count; i++) { Console.WriteLine("myList[{0}] : {1}", i, myList[i]); } Console.WriteLine("\nArray Contains: "); // Displaying the elements in arr for (int i = 0; i < arr.Length; i++) { Console.WriteLine("arr[{0}] : {1}", i, arr[i]); } } } |
Runtime Error:
Unhandled Exception:
System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array’s lower bounds
Parameter name: destinationArray
Reference:
Recommended Posts:
- C# | Copying the elements of ArrayList to a new array
- C# | How to copy the entire ArrayList to a one-dimensional Array
- C# | Getting an enumerator for the entire ArrayList
- C# | Reverse the order of the elements in the entire ArrayList or in the specified range
- C# | Get or set the element at the specified index in ArrayList
- C# | Insert an element into the ArrayList at the specified index
- C# | Remove the element at the specified index of the ArrayList
- C# | Copy the entire LinkedList<T> to Array
- Copying the Queue elements to 1-D Array in C#
- C# | Copying the Collection<T> elements to an array
- C# | Copying BitArray elements to an Array
- C# | Copying the Hashtable elements to an Array Instance
- C# | Copying the HybridDictionary entries to an Array Instance
- C# | Copying the SortedList elements to an Array Object
- C# | Array vs 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.



