在 C# 中将 ArrayList 转换为 Array
csharpprogrammingserver side programming更新于 2025/10/8 9:45:00
要将 ArrayList 转换为 Array,请使用 C# 中的 ToArray() 方法。
首先,设置一个 ArrayList −
ArrayList arrList = new ArrayList();
arrList.Add("one");
arrList.Add("two");
arrList.Add("three");
现在,要进行转换,请使用 ToArray() 方法 −
arrList.ToArray(typeof(string)) as string[];
让我们看看完整的代码 −
示例
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);
}
}
}
输出
one two three

