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

后端 未结 7 734
无人及你
无人及你 2021-01-03 21:31

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

相关标签:
7条回答
  • 2021-01-03 21:40

    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)
    
    0 讨论(0)
  • 2021-01-03 21:43

    Try do that with ToArray() method.

    ArrayList a= new ArrayList(); //your ArrayList object
    var array=(String[])a.ToArray(typeof(string)); // your array!!!
    
    0 讨论(0)
  • 2021-01-03 21:50

    use .ToArray(Type)

    string[] stringArray = (string[])arrayList.ToArray(typeof(string));
    
    0 讨论(0)
  • 2021-01-03 21:55

    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 ) );
    
    0 讨论(0)
  • 2021-01-03 21:58
    string[] myArray = (string[])myarrayList.ToArray(typeof(string));
    
    0 讨论(0)
  • 2021-01-03 22:02

    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);
    
    0 讨论(0)
提交回复
热议问题