如何在 C# 中将 ArrayList
转换为 string[]
?
如何在 C# 中将 ArrayList
转换为 string[]
?
string[] myArray = (string[])myarrayList.ToArray(typeof(string));
ArrayList myAL = new ArrayList();
// Add stuff to the ArrayList.
String[] myArr = (String[]) myAL.ToArray( typeof( string ) );
using System.Linq;
public static string[] Convert(this ArrayList items)
{
return items == null
? null
: items.Cast<object>()
.Select(x => x == null ? null : x.ToString())
.ToArray();
}
using System.Linq;
。另外,我漏掉了一个 .Cast<object>()
的调用。 - NuffinToArray()
方法来实现这个操作。ArrayList a= new ArrayList(); //your ArrayList object
var array=(String[])a.ToArray(typeof(string)); // your array!!!
strArrayList.CopyTo(strArray)
另一种方法如下。
System.Collections.ArrayList al = new System.Collections.ArrayList();
al.Add("1");
al.Add("2");
al.Add("3");
string[] asArr = new string[al.Count];
al.CopyTo(asArr);