在 C# 中将 ArrayList 转换成数组
要在 C# 中将 ArrayList 转换成数组,请使用 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP