To convert an ArrayList to Array, use the ToArray() method in C#.
Firstly, set an ArrayList −
ArrayList arrList = new ArrayList();
arrList.Add("one");
arrList.Add("two");
arrList.Add("three");Now, to convert, use the ToArray() method −
arrList.ToArray(typeof(string)) as string[];
Let us see the complete code −
Example
using System;
using System.Collections;
public class Program {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("one");
arrList.Add("two");
arrList.Add("three");
string[] arr = arrList.ToArray(typeof(string)) as string[];
foreach (string res in arr) {
Console.WriteLine(res);
}
}
}Output
one two three