ArrayList Capacity property in asp.net c#

ArrayList Capacity Property
.Net framework ArrayList Capacity property allow us to get or set the number of elements that the ArrayList can contain. this arraylist Capacity property exists in System.Collections namespace.

arraylist Capacity property return value data type is System.Int32. this return integer value represents the number of elements that the ArrayList can contain.

arraylist Capacity property throw ArgumentOutOfRangeException exception, if 'Capacity'is set to a value that is less than Count. this property also throw OutOfMemoryException exception, if there is not enough memory available on the system.

Capacity is the number of elements that the arraylist can store and Count is the number of elements that are actually exists in arraylist. Capacity is always greater than or equal to Count. Capacity is automatically increased by reallocating the internal array before adding new elements and copying the old elements.

the following asp.net c# example code demonstrate us how can we get or set arraylist Capacity propgrammatically at run time in an asp.net application.
ArrayListCapacityProperty.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() { "Snow","Indigo","FloralWhite","Gold","Red","Green" };

        Label1.Text = "ArrayList Capacity: " + colors.Capacity;

        Label1.Text += "<br /><br /><font color=DodgerBlue>ArrayList Elements... ";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }
        Label1.Text += "</font>";

        colors.Capacity = 25;

        Label1.Text += "<br /><br />Now ArrayList Capacity: " + colors.Capacity;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ArrayList Capacity - How to get or set number of elements that ArrayList can contain</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList Capacity Property
            <br /> How to get or set the number
            <br /> of elements that the ArrayList can contain
        </h2>
        <hr width="450" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="DeepPink"
             Font-Size="Large"
             Font-Names="Courier New"
             Font-Italic="true"
             Font-Bold="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test ArrayList Capacity Property"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples