ArrayList CopyTo(Array) method in asp.net c#

ArrayList CopyTo(Array) Method
.Net framework ArrayList.CopyTo(Array) method allow us to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. this arraylist CopyTo() method exists under System.Collections namespace.

this method require to pass a parameter named 'array'. the 'array' parameter value type is System.Array which represents the one-dimensional Array that is the destination of the elements copied from ArrayList. the Array must have zero-based indexing.

the CopyTo() method has three exceptions. this method throw ArgumentNullException exception, if the 'array' is null. CopyTo() method throw ArgumentException exception, if the 'array' is multi-dimensional or the number of elements in the source arraylist is greater than the number of elements that the destination 'array' can contain. arraylist CopyTo() method throw InvalidCastException exception, if the type of the source arraylist cannot be cast automatically to the type of the destination array.

the following asp.net c# example code demonstrate us how can we copy an arraylist to a compatible one dimensional array programmatically at run time in an asp.net application.
ArrayListCopyToMethod.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() {"Orange","Tomato","Salmon"};

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

        string[] favoriteColors = {"Green","SpringGreen","MediumGreen","SeaGreen","LawnGreen"};
        Label1.Text += "<br /><br />Array Elements...";
        foreach (string color in favoriteColors)
        {
            Label1.Text += "<br />" + color;
        }

        colors.CopyTo(favoriteColors);
        Label1.Text += "<br /><br />After Call CopyTo() Method; Array Elements...";
        foreach(string color in favoriteColors)
        {
            Label1.Text += "<br />" + color;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ArrayList CopyTo() - How to copy the entire ArrayList to a compatible one-dimensional Array</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList CopyTo() Method
            <br /> How to copy the entire ArrayList to a compatible one-dimensional Array
        </h2>
        <hr width="725" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="RosyBrown"
             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 CopyTo() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples