Generic List Add() Method
.Net framework generic list represents a strongly typed list of objects that can be accessed by index. generic list class Add() method allow
us to add an object to the end of the List<T>. this Add(T) method exists in System.Collections.Generic namespace.
this method require to pass a parameter name 'item'. this parameter value type is 'T'. parameter 'T' represents the object to be added to the end of the List<T>. this parameter value can be null for reference types and it allow duplicate elements. this method implements as ICollection<T>.Add(T).
the following asp.net c# example code demonstrate us how can we add an object to the end of generic list in an asp.net application.
this method require to pass a parameter name 'item'. this parameter value type is 'T'. parameter 'T' represents the object to be added to the end of the List<T>. this parameter value can be null for reference types and it allow duplicate elements. this method implements as ICollection<T>.Add(T).
the following asp.net c# example code demonstrate us how can we add an object to the end of generic list in an asp.net application.
GenericListAddMethod.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>();
colors.Add("IndianRed");
colors.Add("Magenta");
colors.Add("DarkSeaGreen");
colors.Add("Pink");
BulletedList1.DataSource = colors;
BulletedList1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Generic List - How to add an object to the end of List</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Crimson; font-style:italic;">
System.Collections.Generic.List Add() method
<br /> How to add an object to the end of List
</h2>
<hr width="425" align="left" color="Pink" />
<asp:BulletedList
ID="BulletedList1"
runat="server"
Width="400"
BackColor="SeaGreen"
ForeColor="Snow"
Font-Size="X-Large"
>
</asp:BulletedList>
<br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Create Generic List And Add Objects And Populate BulletedList"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>
