Initializing a Generic List in asp.net c#

Initializing Generic List
Generic list represents a strongly typed list of objects that can be accessed by index. List class exists under System.Collections.Generic namespace. .Net framework generic list has three constructors, those are List<T>(), List<T>(IEnumerable<T>) and List<T>(Int32). generic list type parameter is 'T' that represents the type of elements in the list.

List<T>() constructor initialize a new instance of the List<T> class that is empty and has the default initial capacity.

List<T>(IEnumerable<T>) constructor initialize a new instance of the List<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied. this constructor need to pass a parameter named 'collection'. this 'collection' parameter type is System.Collections.Generic.IEnumerable<T>. this 'collection' represents the collection whose elements are copied to the new list. this constructor throw an exception named ArgumentNullException if the 'collection' is null.

List<T>(Int32) constructor initialize a new instance of the List<T> class that is empty and has the specified initial capacity. this constructor has a required parameter named 'capacity'. this parameter value data type is System.Int32 which value represents the number of elements that the new list can initially store. this constructor throw ArgumentOutOfRangeException exception if the 'capacity' is less than zero.

the following asp.net c# example code demonstrate us how can we create and use a generic list in an asp.net application.
HowToUseGenericList.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>() {"GoldenRod","DarkViolet","OliveDrab"};
        colors.Add("CornFlowerBlue");
        colors.Add("Yellow");
        colors.Sort();

        RadioButtonList1.DataSource = colors;
        RadioButtonList1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Generic List - How to create and use List</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:OliveDrab; font-style:italic;">
            System.Collections.Generic.List
            <br /> How to create and use List in asp.net
        </h2>
        <hr width="425" align="left" color="Olive" />
        <asp:RadioButtonList
             ID="RadioButtonList1"
             runat="server"
             BorderWidth="2"
             BorderColor="HotPink"
             BorderStyle="Dashed"
             Font-Names="Comic Sans MS"
             Font-Italic="true"
             ForeColor="Snow"
             BackColor="Crimson"
             Width="450"
             RepeatColumns="2"
             >
        </asp:RadioButtonList>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Create Generic List And Populate RadioButtonList"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples