How to convert ArrayList into string array(string[]) in c#

蹲街弑〆低调 提交于 2019-12-30 05:37:08

问题


How can I convert ArrayList into string[] in C#?


回答1:


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



回答2:


A simple Google or search on MSDN would have done it. Here:

ArrayList myAL = new ArrayList(); 

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



回答3:


use .ToArray(Type)

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



回答4:


Try do that with ToArray() method.

ArrayList a= new ArrayList(); //your ArrayList object
var array=(String[])a.ToArray(typeof(string)); // your array!!!



回答5:


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



回答6:


You can use CopyTo method of ArrayList object.

Let's say that we have an arraylist, which has String Type as Elements.

strArrayList.CopyTo(strArray)



回答7:


Another way is as follows.

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


来源:https://stackoverflow.com/questions/8924869/how-to-convert-arraylist-into-string-arraystring-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!