c# - How to remove all items from an ArrayList

ArrayList.Clear() Method
.Net framework ArrayList.Clear() method allow us to remove all elements from the ArrayList. this arraylist Clear() method exists under System.Collections namespace. this method has no required or optional parameter. the Clear() method implements as IList.Clear().

the arraylist Clear() method throw NotSupportedException exception, if the arraylist is read-only or the arraylist has a fixed size. this method set the Count is zero and references to other objects from elements of the collection are also released. but the Capacity remain unchanged in arraylist.

the following asp.net c# example code demonstrate us how can we remove all elements from the arraylist programmatically at run time in an asp.net application.
ArrayListClearMethod.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() {"Teal","Plum","Peru","Pink"};
       
        Label1.Text = "ArrayList Elements....";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }

        //this line remove all elements from the ArrayList.
        colors.Clear();
        Label1.Text += "<br /><br />After Call Clear() Method; ArrayList Elements....";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }
    }
</script>

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