c# - ArrayList Capacity property

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.

<%@ 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;

<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>
    <form id="form1" runat="server">
        <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
        <hr width="450" align="left" color="Navy" />
        <br />
             Font-Names="Courier New"
        <br /><br />
            Text="Test ArrayList Capacity Property"
More asp.net examples