c# - How to remove a range of elements from ArrayList

Remove a range of elements from an ArrayList
The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements.

The following .net c# tutorial code demonstrates how we can remove a range of elements from an ArrayList instance at once. That means we will delete multiple elements from an ArrayList object. Here we will use the ArrayList class RemoveRange() method to remove a range of elements from an ArrayList instance.

The ArrayList RemoveRange(Int32, Int32) method removes a range of elements from the ArrayList. The RemoveRange(int index, int count) method has two parameters. The index parameter is the zero-based starting index of the range of elements to remove. And the count parameter is the number of elements to remove.

The RemoveRange(int index, int count) method throws ArgumentOutOfRangeException if the index is less than zero or the count is less than zero. It throws ArgumentException if the index and count do not denote a valid range of elements in the ArrayList. It also throws NotSupportedException if the ArrayList is read-only or the ArrayList has a fixed size.

So finally, using the ArrayList class RemoveRange() method the .net c# developers can delete multiple elements from an ArrayList instance at once. It removes a range of elements from an ArrayList object.
ArrayListRemoveRangeMethod.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() { "Pink", "Peru", "Plum", "Snow", "Blue","White" };

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

        colors.RemoveRange(2,3);

        Label1.Text += "<br /><br />After Call RemoveRange(index 2, count 3) Method";
        Label1.Text += "<br />Now ArrayList Elements....";
        Label1.Text += "<font color=DarkSlateGray>";
        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 a range of elements from ArrayList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList RemoveRange() Method
            <br /> How to remove a range of elements from the ArrayList
        </h2>
        <hr width="575" 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 RemoveRange() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples