c# - How to remove an item from ArrayList

ArrayList.Remove() method
.Net framework ArrayList.Remove() method allow us to remove the first occurrence of a specific object from the ArrayList. this Remove() method exists under System.Collections namespace.

Remove() method has a required parameter named 'obj'. the 'obj' parameter type is System.Object, which represents the object to remove from the ArrayList. the value can be null.

this method implements as IList.Remove(Object). the Remove() method throw NotSupportedException exception, if the ArrayList is read-only or the ArrayList has a fixed size. if the arraylist does not contain the specified object, the arraylist remain unchanged and no exception is thrown.

the Remove() method determine equality by calling Object.Equals. if the arraylist contains same object multiple times then it remove the first occurrence only.

the following asp.net c# example code demonstrate us how can we remove an object (element) from the arraylist programmatically at run time in an asp.net application.
ArrayListRemoveMethod.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() { "Yellow", "Violet", "Yellow", "DarkGray", "DeepPink" };

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

        colors.Remove("Yellow");

        Label1.Text += "<br /><br />After Call Remove(object Yellow) Method";
        Label1.Text += "<font color=Red>";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }
        Label1.Text += "</font>";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to remove first occurrence of a specific object from ArrayList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList Remove() Method
            <br /> How to remove the first occurrence of a specific object from the ArrayList
        </h2>
        <hr width="675" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="Orchid"
             Font-Size="Large"
             Font-Names="Courier New"
             Font-Italic="true"
             Font-Bold="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test ArrayList Remove() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples