C# ArrayList Insert

Description

ArrayList Insert inserts an element into the ArrayList at the specified index.

Syntax

ArrayList.Insert has the following syntax.


public virtual void Insert(
  int index,
  Object value
)

Parameters

ArrayList.Insert has the following parameters.

  • index - The zero-based index at which value should be inserted.
  • value - The Object to insert. The value can be null.

Returns

ArrayList.Insert method returns

Example

The following code example shows how to insert elements into the ArrayList.


using System;//w w w  . ja v  a 2  s. co  m
using System.Collections;

class MainClass {

    public static void Main() {
        ArrayList al = new ArrayList(5);

        // Add three elements to the end of the array
        al.Add(10);
        al.Add(9);
        al.Add(8);

        // Now, insert three elements in the front of the array
        al.Insert(0, 1);
        al.Insert(0, 2);
        al.Insert(0, 3);

        // Finally, insert into some random spots
        al.Insert(2, 4);
        al.Insert(4, 5);
        al.Insert(6, 6);


        // Enumerate the array
        foreach (int i in al) {
            Console.WriteLine("Entry {0}", i);
        }

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack