如何在C#中将ArrayList转换为字符串数组(string )

如何在C#中将ArrayList转换为string[]

 string[] myArray = (string[])myarrayList.ToArray(typeof(string)); 

一个简单的谷歌或MSDN上的搜索就可以做到。 这里:

 ArrayList myAL = new ArrayList(); // Add stuff to the ArrayList. String[] myArr = (String[]) myAL.ToArray( typeof( string ) ); 

尝试使用ToArray()方法。

 ArrayList a= new ArrayList(); //your ArrayList object var array=(String[])a.ToArray(typeof(string)); // your array!!! 
 using System.Linq; public static string[] Convert(this ArrayList items) { return items == null ? null : items.Cast() .Select(x => x == null ? null : x.ToString()) .ToArray(); } 

使用.ToArray(类型)

 string[] stringArray = (string[])arrayList.ToArray(typeof(string)); 

您可以使用ArrayList对象的CopyTo方法。

假设我们有一个arraylist,它有String Type作为Elements。

 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);