c# - How to insert an item to the ArrayList

ArrayList Insert() Method
.Net framework ArrayList Insert() method allow us to insert an element into the ArrayList at the specified index. this arraylist Insert() method exists under System.Collections namespace. this method require to pass two parameters named 'index' and 'value'.

the 'index' parameter value data type is System.Int32. this integer value represents the zero-based index at which 'value' should be inserted. the 'value' parameter value type is System.Object which represents the Object to insert. the value can be null. this arraylist Insert() method implements as IList.Insert(Int32, Object).

Insert() method throw ArgumentOutOfRangeException exception, if 'index' is less than zero or 'index' is greater than count. this method also throw NotSupportedException 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.

the following asp.net c# example code demonstrate us how can we insert an element into the arraylist at the specified index programmatically at run time in an asp.net application.
ArrayListInsertMethod.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() {"BurlyWood","Coral","Indigo","CadetBlue"};

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

        colors.Insert(1,"Orchid");

        Label1.Text += "<br /><br />After call Insert(index 1, object Orchid) Method";
        Label1.Text+= "<br /><u>Now ArrayList Elements</u>...";
        Label1.Text += "<font color=OrangeRed>";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }
        Label1.Text += "</font>";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to insert an element into the ArrayList at the specified index</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList Insert() Method
            <br /> How to insert an element into the ArrayList at the specified index
        </h2>
        <hr width="650" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="OliveDrab"
             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 Insert() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples