Generic List CopyTo() method in asp.net c#

Generic List CopyTo() Method
.Net framework generic list CopyTo() method allow us to copy the entire List<T> to a compatible one-dimentional array, starting at the beginning of the target array. the List class CopyTo() method exists under System.Collections.Generic namespace.

CopyTo() method has a required parameter named 'array'. the 'array' parameter value type is T[] which represents the one dimentional array that is the destination of the elements copied from List<T>. the array must have zero based indexing.

this method throw two exceptions. CopyTo() method throw ArgumentNullException if the 'array' is null. CopyTo() method throw ArgumentException exception if the number of elements in the source generic list is greater than the number of elements that the destination 'array' can contain. the generic list CopyTo() method uses Array.Copy to copy the elements.

the following asp.net c# example code demonstrate us how can we copy the generic list elements to an array programmatically at run time in an asp.net application.
GenericListCopyToMethod.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        List<string> colors = new List<string>();
        colors.Add("Orchid");
        colors.Add("Green");
        colors.Add("Blue");
        colors.Add("Yellow");

        string[] colorArray = new string[colors.Capacity];
        colors.CopyTo(colorArray);

        RadioButtonList1.DataSource = colorArray;
        RadioButtonList1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Generic List CopyTo() - How to Copy the List to an array</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:SaddleBrown; font-style:italic;">
            System.Collections.Generic.List CopyTo() method
            <br /> How to Copy the List to an array
        </h2>
        <hr width="500" align="left" color="Gray" />
        <asp:RadioButtonList
             ID="RadioButtonList1"
             runat="server"
             BorderWidth="2"
             BorderColor="CornflowerBlue"
             BorderStyle="Dotted"
             Font-Names="Comic Sans MS"
             Font-Italic="true"
             ForeColor="Snow"
             BackColor="Navy"
             Width="475"
             RepeatColumns="2"
             >
        </asp:RadioButtonList>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Copy Generic List To Array And Populate RadioButtonList With Array"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples