ASP.NET ArrayList

The ArrayList is a versatile collection that provides a straightforward means of managing a list of values. It offers a range of operations such as adding, inserting, deleting, and viewing elements, making it highly convenient for data manipulation.

Add Items

VB.Net
Dim aList As New ArrayList aList.Add("Sunday") aList.Add("Monday")
C#
ArrayList aList = new ArrayList(); aList.Add("Sunday"); aList.Add("Monday");

The capacity of an ArrayList refers to the number of elements it can accommodate. As elements are added to an ArrayList, the capacity dynamically expands through automatic reallocation to meet the growing requirements. This automatic capacity adjustment ensures efficient memory allocation and utilization. Conversely, the capacity can be reduced by explicitly calling the TrimToSize method or setting the Capacity property.

Insert items

We can insert items in an ArrayList with a specified index

VB.Net
aList.Insert(2, "Tuesday")
C#
aList.Insert(2, "Tuesday");

The ArrayList collection accommodates a wide range of values, including the acceptance of the "Nothing" value as a valid element. Additionally, it allows for the presence of duplicate elements, providing flexibility in managing data with varying degrees of uniqueness. The dynamic nature of the ArrayList is especially advantageous as it allows elements to be added without requiring prior knowledge of the size, enabling the collection to dynamically grow as needed. Furthermore, it also has the capability to shrink in size when required, further enhancing its flexibility and memory efficiency.

Removing items

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>
Full Source | C#
using System; using System.Collections; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { ArrayList aList = new ArrayList(); aList.Add("Sunday"); aList.Add("Monday"); aList.Add("Tuesday"); aList.Add("Wednesday"); aList.Add("Thurday"); aList.Add("Friday"); aList.Add("Saturday"); ListBox1.DataSource = aList; 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 aList As New ArrayList aList.Add("Sunday") aList.Add("Monday") aList.Add("Tuesday") aList.Add("Wednesday") aList.Add("Thurday") aList.Add("Friday") aList.Add("Saturday") ListBox1.DataSource = aList ListBox1.DataBind() End Sub End Class