22

I have an array of strings. How can I convert it to System.Collections.ArrayList?

2
  • 2
    Is there a particular reason you need to use an ArrayList? Use List<string> instead. Commented Nov 9, 2009 at 15:40
  • Why not just leave it as the Array (or more accurately, IEnumerable<string>) and use the LINQ Extensions? Commented Nov 9, 2009 at 16:17

4 Answers 4

46
string[] myStringArray = new string[2];
myStringArray[0] = "G";
myStringArray[1] = "L";

ArrayList myArrayList = new ArrayList();
myArrayList.AddRange(myStringArray);
Sign up to request clarification or add additional context in comments.

Comments

45

Just use ArrayList's constructor:

var a = new string[100]; 
var b = new ArrayList(a);

2 Comments

The lowest of low hanging fruit.
Appears cleaner and a smaller code-footprint than the accepted .AddRange() approach.
3

System.Collections.ArrayList list = new System.Collections.ArrayList( new string[] { "a", "b", "c" } );

Comments

1
public stringList[] = {"one", "two", "three"};
public ArrayList myArrayList;

     myArrayList = new ArrayList();
     foreach (string i in stringList)
     {
         myArrayList.Add(i);
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.