c# - How to reverse ArrayList range of elements order

Reverse order of a range of elements in 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 reverse the order of a range of elements in an ArrayList instance. That means we will reverse the order of the elements in a specified range within an ArrayList object. Here we will use the ArrayList class Reverse() method to reverse a range of element’s order.

The ArrayList Reverse() method reverses the order of the elements in the ArrayList or a portion of it. The ArrayList class Reverse(Int32, Int32) method overload reverses the order of the elements in the specified range.

The ArrayList Reverse(int index, int count) method has two parameters. The index parameter is The zero-based starting index of the range to reverse. And the count parameter is The number of elements in the range to reverse.

The ArrayList Reverse(int index, int count) method throws several exceptions. It throws ArgumentOutOfRangeException if the index is less than zero or the count is less than zero. The method throws ArgumentException if the index and count do not denote a valid range of elements in the ArrayList. And it also throws NotSupportedException if the ArrayList is read-only.
ArrayListReverseMethodWithRange.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() { "DimGray", "Gold", "Green", "Ivory", "LightSalmon" };

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

        colors.Reverse(1,3);

        Label1.Text += "<br /><br />After Call Reverse(index 1, count 3) Method";
        Label1.Text += "<br />Now ArrayList Elements....";

        Label1.Text += "<font color=DeepPink>";
        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>ArrayList - How to reverse the order of the elements in the specified range</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList Reverse(Int32, Int32) Method
            <br /> How to reverse the order of the elements in the specified range
        </h2>
        <hr width="625" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="DarkSalmon"
             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 Reverse(Int32, Int32) Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples