ArrayList colors = new ArrayList(); colors.Add("red"); colors.Add("green"); colors.Add("blue"); string[] newColors = new string[3]; colors.CopyTo(newColors, 0); foreach (string c in newColors) { Console.WriteLine(c); }
ArrayList numbers = new ArrayList(); numbers.Add(1); numbers.Add(2); numbers.Add(3); int[] newNumbers = new int[5]; numbers.CopyTo(newNumbers, 2); foreach (int n in newNumbers) { Console.WriteLine(n); }This example creates an ArrayList of integer values (representing numbers) and adds three elements to it. Then, a new int array of size 5 is created. Finally, the CopyTo() method is used to copy the elements of the ArrayList to the new array starting at index 2. The values in the new array are then printed to the console. Both of these examples use the CopyTo() method from the System.Collections.ArrayList package/library available in C#.