Để chuyển đổi ArrayList thành Array, hãy sử dụng phương thức ToArray () trong C #.
Đầu tiên, đặt ArrayList -
ArrayList arrList = new ArrayList();
arrList.Add("one");
arrList.Add("two");
arrList.Add("three"); Bây giờ, để chuyển đổi, hãy sử dụng phương thức ToArray () -
arrList.ToArray(typeof(string)) as string[];
Hãy cho chúng tôi xem mã hoàn chỉnh -
Ví dụ
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);
}
}
} Đầu ra
one two three