convert string array to arraylist


In this article, we will learn how to convert string array into an arraylist. In the previous article we have seen how to convert arraylist to string array.

In the code given below, we declare a string array and assign values to each element in the array.



Next, we create a new arraylist of type string. We use AddRange method we can convert string array to an arraylist. We need to pass the array to the AddRange method as a parameter as shown in the code given below.

public partial class _Default : System.Web.UI.Page
{
   string connectionString = "Data Source=LocalHost; Initial CataLog=ERPFinAccounting; User ID=kris; Password=XXX";
   protected void Page_Load(object sender, EventArgs e)
   {
		ArrayList StringArrayList = ConvertStringArrayToArrayList();
        string str = string.Empty;
	    foreach (string name in StringArrayList)
        {
            str += name + "\n";
        }
    }
}

public ArrayList ConvertStringArrayToArrayList()
{
    string[] myArray = new string[5];   
    myArray[0] = "Jhon";
    myArray[1] = "Jill";
    myArray[2] = "JO";
    myArray[3] = "Chris";

    ArrayList StringArraytoArrayList = new ArrayList(myArray.Length);
    StringArraytoArrayList.AddRange(myArray);
    return StringArraytoArrayList;
}