c# - How to insert multiple elements into an ArrayList

Insert multiple elements into an ArrayList at once
The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements.

The following .net c# tutorial code demonstrates how we can insert multiple elements into an ArrayList at once. That means we add a collection of elements into an ArrayList at the specified index position. Here we will use the ArrayList class InsertRange() method to add multiple elements to an ArrayList instance.

The ArrayList InsertRange(Int32, ICollection) method inserts the elements of a collection into the ArrayList at the specified index. The ArrayList InsertRange(int index, System.Collections.ICollection c) method has two parameters. The index parameter is the zero-based index at which the new elements should be inserted. And the c parameter is the ICollection whose elements should be inserted into the ArrayList. The collection itself cannot be null, but it can contain elements that are null.

The ArrayList InsertRange(index, c) method throws several exceptions. The method throws ArgumentNullException if the c is null. It throws ArgumentOutOfRangeException if the index is less than zero or the index is greater than Count. This method throws NotSupportedException if the ArrayList is read-only or the ArrayList has a fixed size.

So finally, using the ArrayList class InsertRange() method the .net c# developers can add multiple elements to an ArrayList instance at once at its specified index position.
ArrayListInsertRangeMethod.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ArrayList colors = new ArrayList() {"Green","SeaGreen","SpringGreen","LawnGreen"};

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

        List<string> redColors = new List<string>() {"Red","DarkRed","IndianRed" };
        colors.InsertRange(2,redColors);

        Label1.Text += "<br /><br />After call InsertRange(index 2, ICollection) Method";
        Label1.Text+= "<br /><u>Now ArrayList Elements</u>...";
        Label1.Text += "<font color=Red>";
        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 the elements of a collection 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 InsertRange() Method
            <br /> How to insert the elements of a collection
            <br /> into the ArrayList at the specified index
        </h2>
        <hr width="475" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="Orchid"
             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 InsertRange() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples