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

Generic List GetRange() Method
.Net framework generic list GetRange() method allow us to create a shallow copy of a range of elements in the source List<T>. the GetRange() method is exists under System.Collections.Generic namespace. this method require to pass two parameters, named 'index' and 'count'.

both 'index' and 'count' parameters data type is System.Int32. the 'index' represent the zero based List<T> index at which the range starts and the 'count' represent the number of elements in the range.

this method return value type is System.Collections.Generic.List<T>. this returned List<T> is a shallow copy of a range of elements in the source List<T>. this method throw ArgumentOutOfRangeException exception if the 'index' is less than zero (0) or 'count' is less than 0. this method also throw ArgumentException exception if the 'index' and 'count' do not denote a valid range of elements in the List<T>.

the following asp.net c# example code demonstrate us how can we get a shallow copy of a range of elements in the source generic list in an asp.net application.
GenericListGetRangeMethod.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>() {"DarkRed","Purple","MediumPurple"};
        colors.Add("Maroon");
        colors.Add("MediumBlue");
        colors.Add("Orange");
        colors.Add("OrangeRed");

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

        string[] colorArray = colors.GetRange(2, 3).ToArray();

        Label1.Text += "<br /><br />After Call GetRange(2,3) Method Array Elements are.....<br />";

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Generic List GetRange() - How to create a shallow copy of a range of elements in the source List</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:SlateBlue; font-style:italic;">
            System.Collections.Generic.List GetRange() Method
            <br /> How to create a shallow copy of a range of elements in the source List
        </h2>
        <hr width="650" align="left" color="Purple" />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="DarkOrchid"
             Font-Size="Large"
             Font-Names="Courier New"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Generic List GetRange() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples