c# - How to count the elements of an ArrayList

ArrayList Count Property
.Net framework ArrayList class implements the IList interface using an array whose size is dynamically increased as required. ArrayList.Count() property allow us to get the number of elements actually contained in the ArrayList. this ArrayList Count property exists in System.Collections namespace.

arraylist Count property return value data type is System.Int32. this integer value represents the number of elements actually contained in the arraylist. this property implements as ICollection.Count. Capacity is always greater than or equal to Count property value. Capacity is the number of elements that the arraylist can store.

the following asp.net c# example code demonstrate us how can we get the number of elements exists in arraylist programmatically at run time in an asp.net application.
ArrayListCountProperty.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() { "IndianRed","Red","DarkRed" };

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

        Label1.Text += "<br />ArrayList Elements Count: " + colors.Count;
        
        colors.Add("DodgerBlue");

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

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ArrayList Count - How to get number of elements actually contained in the ArrayList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList Count Property
            <br /> How to get the number of elements
            <br /> actually contained in the ArrayList
        </h2>
        <hr width="375" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="HotPink"
             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 Count Property"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples