How to Convert Arraylist to Array in .net

Array is the group or collection of similar datatypes object and it is member of the system namespace whereas ‘Arraylist’ is the group of variables or object with different datatypes and it implement the ‘IList’ interface using an array whose size is dynamically increased as required.

The ArrayList class provides ‘ToArray()’ and ‘ToArray(type)’ methods to convert array list object to array. If you want to convert array list to array then you can use the following codes:

Convert ArrayList to Array of objects

If you don’t give the specified type as a parameter in ToArray() method, it copies the elements of the ArrayList to a new Object array. In the below ‘ArrObjects’ is the array of object. See below:

VB.net Code

Private Sub ConvertArrayListToArray()
        Dim Arrlist As New ArrayList
        Arrlist.Add("Delhi")
        Arrlist.Add("Mumbai")
        Arrlist.Add("NewYork")
        Arrlist.Add("Paris")
        Arrlist.Add("Singapur")
        Arrlist.Add("Boston")
 
        ' Array of objects.
        Dim ArrObjects As Array
        ArrObjects = Arrlist.ToArray()
    End Sub

C# Code

private void ConvertArrayListToArray()
{
      ArrayList Arrlist = new ArrayList();
      Arrlist.Add("Delhi");
      Arrlist.Add("Mumbai");
      Arrlist.Add("NewYork");
      Arrlist.Add("Paris");
      Arrlist.Add("Singapur");
      Arrlist.Add("Boston");
 
      // Array of objects.
      Array ArrObjects = null;
      ArrObjects = Arrlist.ToArray();
}

Convert ArrayList to Array of specified element type

‘ToArray(type)’ method copies the elements of the ArrayList to a new array of the specified element type, All of the objects in the ArrayList object will be cast to the Type specified in the type parameter. See below:

VB.net Code

Private Sub ConvertArrayListToArray()
 
        Dim Arrlist As New ArrayList
        Arrlist.Add("Delhi")
        Arrlist.Add("Mumbai")
        Arrlist.Add("NewYork")
        Arrlist.Add("Paris")
        Arrlist.Add("Singapur")
        Arrlist.Add("Boston")
 
        ‘ array of string
        Dim ArrString() As String
        ArrString = DirectCast(Arrlist.ToArray(GetType(String)), String())
 
        ' Array of objects.
        Dim ArrObjects As Array
        ArrObjects = Arrlist.ToArray()
    End Sub

C# Code

private void ConvertArrayListToArray()
{
 
      ArrayList Arrlist = new ArrayList();
      Arrlist.Add("Delhi");
      Arrlist.Add("Mumbai");
      Arrlist.Add("NewYork");
      Arrlist.Add("Paris");
      Arrlist.Add("Singapur");
      Arrlist.Add("Boston");
 
      // array of string
      string[] ArrString = null;
      ArrString = (string[])Arrlist.ToArray(typeof(string));
 
      // Array of objects.
      Array ArrObjects = null;
      ArrObjects = Arrlist.ToArray();
}

If The type of the element in ArrayList cannot be cast automatically to the given type, InvalidCastException exception will be thrown, You need to aware of the element’s type of your ArrayList object before using ToArray(type) method.

In the below example, we are trying to convert an arraylist which contains some mixed content of the string and numbers and when we try to convert this arraylist into array in int type, ‘InvalidCastException’ exception will be thrown. See the try-catch block in below example:

static void Main(string[] args)
    {
 
        ArrayList Arrlist = new ArrayList();
        Arrlist.Add("3453");
        Arrlist.Add("235");
        Arrlist.Add("NewYork");
        Arrlist.Add("2543");
        Arrlist.Add("22");
        Arrlist.Add("1002");
 
        // array of string
        string[] ArrString = null;
        try
        {
            ArrString = (string[])Arrlist.ToArray(typeof(int));
        }
        catch (InvalidCastException invalidCastEx)
        {
            Console.WriteLine(invalidCastEx.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

In the above, ‘Arrlist’ has one string type element ‘NewYork’ where ‘ToArray(typeof(int))’ method fails to cast ‘NewYork’ string into the int type. The above program gives the output as:

At least one element in the source array could not be cast down to the destination array type..