How to Convert ArrayList to Array in Asp.Net C# VB.Net

In many cases developers have to create ArrayList to achieve dynamic array functionality. But sometimes you may need to convert the ArrayList to an Array. For example if you want to transfer CSV data to ORACLE database then if you use bindarray then the conversion is required. Otherwise you won’t be able to insert data into oracle database. This is only one scenario but for lot of reasons you may need such type of conversion. Here in this article i will show you how you can convert ArrayList to Array.

Convert ArrayList to an Array Using C#:

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            ArrayList Dynamic_List = new ArrayList();

            for (int i = 0; i < 5; i++)

            //Convert Here
            int[] array = (int[])Dynamic_List.ToArray(typeof(int));

            for (int i = 0; i <= array.GetUpperBound(0); i++)
                Response.Write("Index="+i.ToString()+" Value="+array[i].ToString() + "</BR>");

Note: Don’t forget to add “System.Collections” namespace.

Convert ArrayList to an Array Using VB.Net:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim Dynamic_List As New ArrayList()

            For i As Integer = 0 To 4
                Dynamic_List.Add(i * 2)

            'Convert Here
            Dim array As Integer() = DirectCast(Dynamic_List.ToArray(GetType(Integer)), Integer())

            For i As Integer = 0 To array.GetUpperBound(0)
                Response.Write("Index=" & i.ToString() & " Value=" & array(i).ToString() & "</BR>")
        End If

    End Sub

Note: Don’t forget to add “System.Collections” namespace.

Convert ArrayList to Array

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *