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

Generic List Remove() Method
.Net framework generic list Remove() method allow us to remove the first occurrence of a specific object from the List<T>. the Remove() method exists under System.Collections.Generic namespace. Remove() method require to pass a parameter named 'item'. the 'item' parameter type is 'T'. this 'T' represents the object to remove from the List<T>.

this method return value data type is System.Boolean. it return 'true', if the specified 'item' is successfully removed. otherwise the method return 'false'. the method also return 'false', if the 'item' was not found in the List<T>. if the list contans same object many times then the method remove the first occurrence from the list.

the following asp.net c# example code demonstrate us how can we remove an element from generic list programmatically in an asp.net application.
GenericListRemoveMethod.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>() {"HoneyDew","HotPink","Indigo"};
        colors.Add("Khaki");
        colors.Add("HotPink");

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

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

        colors.Remove("Khaki");
        Label1.Text += "<br /><br />After Remove 'Khaki': 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 Remove() - How to remove the first occurrence of a specific object from the List</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkSlateGray; font-style:italic;">
            System.Collections.Generic.List Remove() Method
            <br /> How to remove the first occurrence of a specific object from the List
        </h2>
        <hr width="650" align="left" color="SlateBlue" />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="Maroon"
             Font-Size="Large"
             Font-Names="Courier New"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Generic List Remove() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples