Net-informations.com
SiteMap  | About    

ASP.NET ArrayList

ArrayList contains a simple list of values and very easily we can add , insert , delete , view etc. The capacity of a ArrayList is the number of elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation.The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly.

The ArrayList collection accepts Nothing as a valid value, allows duplicate elements. It is very flexible becuse we can add without any size information , that is it grow dynamically and also shrink .

vb.net
  Dim aList As New ArrayList
  aList.Add("Sunday")

aList.Add("Monday")

C#
  ArrayList aList = new ArrayList();
  aList.Add("Sunday");

aList.Add("Monday");

We can insert items in an ArrayList with a specified index

vb.net

  aList.Insert(2, "Tuesday")

C#

  aList.Insert(2, "Tuesday");

For removing items from an ArrayList , we can use ArrayList.Remove(object)

vb.net

  aList.Remove("Monday")

C#

  aList.Remove("Monday");

An ArrayList can also be sorted alphabetically or numerically with the Sort() method.

vb.net

  aList.Sort()

C#

  aList.Sort();

The following ASP.NET program add seven days in a week to an ArrayList and bind it to a ListBox control.

Default.aspx

  

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
		<br />
		<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
	</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.