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

Generic List RemoveRange() Method
.Net framework generic list RemoveRange() method allow us to remove a range of elements from the List<T>. List class RemoveRange() method exists under System.Collections.Generic namespace. this method require to pass two parameters named 'index' and 'count'.

the 'index' parameter value type is System.Int32 which represent the zero-based starting index of the range of elements to remove from list. 'count' parameter value data type also System.Int32 and it represents the number of elements to remove.

this method throw two exceptions named ArgumentOutOfRangeException and ArgumentException. if 'index' is less than zero or 'count' is less than zero then the method throw the ArgumentOutOfRangeException exception. if the 'index' and 'count' do not denote a valid range of elements in the List<T> then the RemoveRange() method throw the ArgumentException exception.

the following asp.net c# example code demonstrate us how can we remove a range of elements from a generic list programmatically in an asp.net application.
GenericListRemoveRangeMethod.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>() {"Maroon","Magenta","Coral"};
        colors.Add("MediumPurple");
        colors.Add("MediumSeaGreen");
        colors.Add("ForestGreen");
        colors.Add("SpringGreen");

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

        colors.RemoveRange(2,3);
        Label1.Text += "<br /><br />After remove a range of elements(from index 2 and count 3)";
        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 RemoveRange() - How to remove a range of elements from the List</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkOliveGreen; font-style:italic;">
            System.Collections.Generic.List RemoveRange() Method
            <br /> How to remove a range of elements from the List
        </h2>
        <hr width="525" align="left" color="OliveDrab" />
        <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 RemoveRange() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples