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

Generic List RemoveAt() Method
.Net framework generic list RemoveAt() method allow us to remove the element at the specified index of the List<T>. the RemoveAt() method exists under System.Collections.Generic namespace. the RemoveAt(index) method require to pass a parameter named 'index'.

the 'index' parameter value data type is System.Int32. this parameter value represents the zero-based index of the element to remove. this method implements as IList<T>.RemoveAt(Int32) and IList.RemoveAt(Int32). RemoveAt() method throw an exception named ArgumentOutOfRangeException, if the 'index' is less than zero or 'index' is equal or greater than Count.

the following asp.net c# example code demonstrate us how can we remove an element by index from generic list in an asp.net application.
GenericListRemoveAtMethod.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>() {"LemonChiffon","LightBlue","LightCoral"};
        colors.Add("Linen");
        colors.Add("Lime");

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

        colors.RemoveAt(2);
        Label1.Text += "<br /><br />After Remove at index 2: List Elements....<br />";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }

        colors.RemoveAt(1);
        Label1.Text += "<br /><br />After Remove at index 1: 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 RemoveAt() - How to remove the element at the specified index of the List</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:PaleVioletRed; font-style:italic;">
            System.Collections.Generic.List RemoveAt() Method
            <br /> How to remove the element at the specified index of the List
        </h2>
        <hr width="625" align="left" color="Violet" />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="DarkOliveGreen"
             Font-Size="Large"
             Font-Names="Courier New"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Generic List RemoveAt() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples