asp.net c# example - generic list reverse range

Generic List Reverse() method with range
.Net framework generic list Reverse(Int32, Int32) method allow us to reverse the order of the generic list elements in the specified range. the Reverse() method exists under System.Collections.Generic namespace. the Reverse(Int32, Int32) method required to pass two parameters named 'index' and 'count'. both parameters value data type is System.Int32.

the 'index' parameter value represents the zero based starting index of the range to reverse. and the 'count' parameter represents the number of elements in the range to reverse.

this method throw two exceptions. Reverse(Int32, Int32) method throw ArgumentOutOfRangeException if the 'index' is less than zero or 'count' is less than zero. this method throw ArgumentException exception if 'index' and 'count' do not denote a valid range of elements in the List<T>. this method uses the Array.Reverse to reverse the order of the elements.

the following asp.net c# example code demonstrate us how can we reverse the order of the generic list elements in the specified range programmatically at run time in an asp.net application.
GenericListReverseRange.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>() {"PapayaWhip","PeachPuff","PaleGreen"};
        colors.Add("RosyBrown");
        colors.Add("RoyalBlue");
        colors.Add("SandyBrown");

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

        colors.Reverse(2,3);
        Label1.Text += "<br /><br />After Call The Reverse(index 2, count 3) Method";
        Label1.Text += "<br />Now List Elements....<br />";

        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color + " | Index = " + colors.IndexOf(color);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Generic List - How to reverse the order of the elements in the specified range</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:SandyBrown; font-style:italic;">
            System.Collections.Generic.List Reverse(Int32, Int32) Method
            <br /> How to reverse the order of the elements in the specified range
        </h2>
        <hr width="550" align="left" color="Orchid" />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="RoyalBlue"
             Font-Size="Large"
             Font-Names="Courier New"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Generic List Reverse() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples