ASP.NET List

The Collection classes form a cohesive set of classes carefully crafted to facilitate the grouping and manipulation of objects. These classes are specifically designed to handle common tasks performed on collections, offering a wide range of functionality and versatility.

The core capabilities of collection classes revolve around fundamental operations such as adding elements to the collection, removing elements from the collection, and obtaining the count or number of elements present within the collection. These operations provide the foundation for managing and manipulating collections effectively.

One notable member of the Collection classes is the List class, which resides within the System.Collections.Generic namespace. The List < T > class in C# represents a specialized form of collection that can store objects of a specific type in a strongly typed manner. It provides a powerful and efficient means of organizing a list of objects that can be accessed using an index.

asp.net-collections

The elements within a List < T > instance can be conveniently accessed using an integer-based index. The indices within this collection are zero-based, meaning the first element in the list is accessed using an index of zero, the second element with an index of one, and so on. This indexing system offers a straightforward and intuitive way to retrieve and manipulate elements within the List.

By utilizing the List < T > class and other collection classes, developers can utilize the full potential of grouping objects and performing operations on them efficiently. These classes offer a comprehensive set of features and methods, making them indispensable tools for managing and manipulating collections in a type-safe and organized manner.

List < T >

The parameter T is the type of elements in the list.

Add items in List collection ?

Add Integer values in the List collection

C#
List < int > intList = new List< int > (); intList.Add(20); intList.Add(30); intList.Add(50); intList.Add(70);
VB.Net
Dim intList As New List(Of Integer)() intList.Add(20) intList.Add(30) intList.Add(50) intList.Add(70)

Add String values in the List

C#
months.Add("January"); months.Add("Frebruary"); months.Add("March"); months.Add("April"); months.Add("May"); months.Add("June");
VB.Net
months.Add("January") months.Add("Frebruary") months.Add("March") months.Add("April") months.Add("May") months.Add("June")

Obtaining the number of elements in a List ?

You can use "count" property to know the number of items in the List collection

months.Count

Retrieve items from List ?

You can retrieve items from List collection using for loops.

foreach loop

C#
foreach (string month in months) { MessageBox.Show(month); }
VB.Net
For Each month As String In months MessageBox.Show(month) Next

for loop

C#
for (int i = 0; i < months.Count; i++) { MessageBox.Show(months[i]); }
VB.Net
For i As Integer = 0 To months.Count - 1 MessageBox.Show(months(i)) Next

Insert an item in the List ?

You can insert an item in the List by specify its index value like (index,item) .

months.Insert(2, "March");

In the above code the month "March" inserted in the index position 2.

Remove an item from List collection ?

Remove() can use to remove item from List collection.

months.Remove("March");

Check if an item contains in the List collection ?

You can use List.Contains() methods to check an item exists in the List

C#
if (months.Contains("March")) { MessageBox.Show("The month March exists in the List"); }
VB.Net
If months.Contains("March") Then MessageBox.Show("The month March exists in the List") End If

Copy an Array to a List collection ?

C#
string[] strArr = new string[3]; strArr[0] = "Sunday"; strArr[1] = "Monday"; strArr[2] = "Tuesday"; //here to copy array to List List < string > arrlist = new List < string > (strArr);
VB.Net
Dim strArr As String() = New String(2) {} strArr(0) = "Sunday" strArr(1) = "Monday" strArr(2) = "Tuesday" 'here to copy array to List Dim arrlist As New List(Of String)(strArr)

Finally clear method remove all the items from List collection.

List.Clear ();

The following program shows some operations in List collections.

asp.net-list Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:ListBox ID="ListBox1" runat="server" Height="129px" Width="200px"></asp:ListBox><br /> <br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </div> </form> </body> </html>
Full Source | C#
using System; using System.Web; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Collections.Generic; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { List < string > months = new List < string > (); //add items in a List collection months.Add("January"); months.Add("Frebruary"); months.Add("March"); months.Add("April"); months.Add("May"); months.Add("June"); //Remove an Item from list months.Remove("March"); //insert an item in the list months.Insert(2, "March"); ListBox1.DataSource = months; ListBox1.DataBind(); } }
Full Source | VB.NET
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim months As New List(Of String)() 'add items in a List collection months.Add("January") months.Add("Frebruary") months.Add("March") months.Add("April") months.Add("May") months.Add("June") 'Remove an Item from list months.Remove("March") 'insert an item in the list months.Insert(2, "March") ListBox1.DataSource = months ListBox1.DataBind() End Sub End Class