c# - How to add an item to the ArrayList

ArrayList Add() Method
.Net framework ArrayList Add() method allow us to add an object to the end of the ArrayList. this arraylist Add() method exists in System.Collections namespace. the Add() method require to pass a parameter named 'value'.

this 'value' parameter value type is System.Object which represents the Object to be added to the end of the arraylist. this value can be null. the Add() method return value type is System.Int32. this integer return value represents the arraylist index at which the 'value' has been added.

this method implements as IList.Add(Object). arralist Add() method throw NoSupportedException exception, if the arraylist is read-only or the arraylist has a fixed size. arraylist accept null as a valid value and it also allow duplicate elements. arraylist Capacity increased automatically by reallocating the internal array.

the following asp.net c# example code demonstrate us how can we add element to the end of arraylist programmatically at run time in an asp.net application.
ArrayListAddMethod.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() {"Red","OliveDrab"};
        colors.Add("MintCream");
        colors.Add("FloralWhite");
        colors.Add("Green");
        colors.Add("White");
       
        Label1.Text = "ArrayList Elements....";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to add an object to the end of the ArrayList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList Add() Method
            <br /> How to add an object to the end of the ArrayList
        </h2>
        <hr width="425" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="SeaGreen"
             Font-Size="Large"
             Font-Names="Courier New"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test ArrayList Add() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples