Net-informations.com
SiteMap  | About    

ASP.NET List

How to use Asp.Net List

The Collection classes are a set of classes designed specifically for grouping together objects and performing tasks on them. The basic functionality of collection classes are Adding elements to the Collection, Removing elements from the collection, Obtaining number of elements in the collection atc. List class is a Collection member and defined in the System.Collections.Generic namespace.

asp.net-collections

The C# List < T > class represents a strongly typed list of objects that can be accessed by index. Elements in the List can be accessed using an integer index. Indexes in this collection are zero-based.

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 by 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>


Click the following links to see full source code

C# Source Code
VB.NET Source Code
Default.aspx.cs
Default.aspx.vb





net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.