ArrayList.ToArray() method with data type
How to copy the elements of the ArrayList to a new array of the specified element type
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Program { static void Main(string[] args) { ArrayList favoriteColors = new ArrayList() { "White", "FloralWhite", "FireBrick", "GoldenRod", "LightCyan" }; Console.WriteLine("ArrayList Elements...."); foreach (string color in favoriteColors) { Console.WriteLine(color); } string[] favoriteColorArray = (string[])favoriteColors.ToArray(typeof(string)); Console.WriteLine(); Console.WriteLine("After Call ToArray(Type string) Method"); Console.WriteLine("Array Elements...."); for (int i = 0; i < favoriteColorArray.Length; i++) { Console.WriteLine(favoriteColorArray[i]); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ArrayList Elements.... White FloralWhite FireBrick GoldenRod LightCyan After Call ToArray(Type string) Method Array Elements.... White FloralWhite FireBrick GoldenRod LightCyan |