c# - How to convert an ArrayList into an Array

ArrayList ToArray() Method
.Net framework ArrayList.ToArray() method allow us to copy the elements of the ArrayList to a new Object array. this arraylist ToArray() method exists in System.Collections namespace. the ToArray() method has no required or optional parameter.

the ToArray() method return value type is System.Object[] which represents an Object array containing copies of the elements of the ArrayList. this arraylist ToArray() method copies the elements to array using Array.Copy.

the following asp.net c# example code demonstrate us how can we copy the arraylist elements to a new array programmatically at run time in an asp.net application.
ArrayListToArrayMethod.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() { "CornFlowerBlue", "DarkGoldenRod", "Gold", "DodgerBlue", "FloralWhite" };

        Label1.Text = "ArrayList Elements....";
        Label1.Text += "<font color=DarkOrange>";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }
        Label1.Text += "</font>";

        var colorArray = colors.ToArray();

        Label1.Text += "<br /><br />After Call ToArray() Method";
        Label1.Text += "<br />Array Elements....";
        Label1.Text += "<font color=DarkSalmon>";
        for (int i = 0; i < colorArray.Length; i++)
        {
            Label1.Text += "<br />" + colorArray[i].ToString();
        }
        Label1.Text += "</font>";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ArrayList ToArray() - How to copy the elements of the ArrayList to a new Object array</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList ToArray() Method
            <br /> How to copy the elements of the ArrayList to a new Object array
        </h2>
        <hr width="650" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="SeaGreen"
             Font-Size="Large"
             Font-Names="Courier New"
             Font-Italic="true"
             Font-Bold="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test ArrayList ToArray() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples