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

Generic List InsertRange() Method
.Net framework generic list InsertRange() method allow us to insert the elements of a collection into the List<T> at the specified index. InsertRange() method exists in System.Collections.Generic namespace. this method has two required parameters named 'index' and 'collection'.

the 'index' parameter value data type is System.Int32 that represents the zero based index at which the new elements should be inserted. the 'collection' parameter value type is System.Collections.Generic.IEnumerable<T> that represents the collection whose elements should be inserted into the List<T>.

the InsertRange() method has two exceptions. it throw ArgumentNullException when the 'collection. is null. the method throw ArgumentOutOfRangeException when 'index' is less than zero or 'index' is greater than Count.

the following asp.net c# example code demonstrate us how can we insert a elements collection into a generic list specified index position programmatically at run time in an asp.net application.
GenericListInsertRangeMethod.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>() {"Green","SeaGreen","SpringGreen"};

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

        string[] redColors = new string[] {"Red","IndianRed","DarkRed" };
        colors.InsertRange(1,redColors);

        Label1.Text += "<br /><br />After inserting 'Red Colors' at index 1";

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Generic List InsertRange() - How to insert the elements of a collection into the List at the specified index</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkGreen; font-style:italic;">
            System.Collections.Generic.List InsertRange() Method
            <br /> How to insert the elements of a collection
            <br /> into the List at the specified index
        </h2>
        <hr width="450" align="left" color="Green" />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="PaleVioletRed"
             Font-Size="Large"
             Font-Names="Courier New"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Generic List InsertRange() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples