Generic List Reverse() method in asp.net c#

Generic List Reverse() Method
.Net framework generic list Reverse() method allow us to reverse the order of the elements in the entire List<T>. the List class Reverse() method is exists under System.Collections.Generic namespace. this method have no parameter.

the Reverse() method uses Array.Reverse (method) to reverse the order of the list elements. this method is a 'n' operation, where 'n' is Count. in the bellow example code we created a String type generic list and populated it by five colors name. then we reverse the order of list elements by calling the generic list Reverse() method.

the following asp.net c# example code demonstrate us how can we reverse the specified generic list elements order in an asp.net application.
GenericListReverseMethod.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>() {"Olive","Orchid","Orange"};
        colors.Add("OladLace");
        colors.Add("Moccasin");

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

        colors.Reverse();
        Label1.Text += "<br /><br />After Call The Reverse() 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 Reverse() - How to reverse the order of the elements in the entire List</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkOliveGreen; font-style:italic;">
            System.Collections.Generic.List Reverse() Method
            <br /> How to reverse the order of the elements in the entire List
        </h2>
        <hr width="525" align="left" color="OliveDrab" />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="Navy"
             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