In ArrayList class, it provides Insert and RemoveAt methods to insert and remove an item at a specific index, but this feature is not available in a standard array (System.Array). The following utility class provide two methods to do the job.

using System;

public class ArrayUtilities
{
    public void InsertIntoArray(Array target,
      object value, int index)
    {
        if (index < target.GetLowerBound(0) ||
            index > target.GetUpperBound(0))
        {
            throw (new ArgumentOutOfRangeException("index", index,
              "Array index out of bounds."));
        }
        else
        {
            Array.Copy(target, index, target, index + 1,
                       target.Length - index - 1);
        }

        target.SetValue(value, index);
    }

    public void RemoveFromArray(Array target, int index)
    {
        if (index < target.GetLowerBound(0) ||
            index > target.GetUpperBound(0))
        {
            throw (new ArgumentOutOfRangeException("index", index,
              "Array index out of bounds."));
        }
        else if (index < target.GetUpperBound(0))
        {
            Array.Copy(target, index + 1, target, index,
                       target.Length - index - 1);
        }

        target.SetValue(null, target.GetUpperBound(0));
    }
}

The InsertIntoArray and RemoveFromArray methods make use of the Array.Copy static method to perform their operations. Initially, both methods test to see whether an item is being added or removed within the bounds of the array target. If the item passes this test, the Array.Copy method is used to shift items around to either make room for an element to be inserted or to overwrite an element being removed from the array.

The RemoveFromArray method accepts two parameters. The first parameter, target, is the array from which an element is to be removed; the second parameter, index, is the zero-based position of the element to be removed in the array. Elements at and above the inserted element are shifted down by one. The last element in the array is set to the default value for the array type.

The InsertIntoArray method accepts three parameters. The first parameter, target, is the array that is to have an element added, value is the element to be added, and index is the zero-based position at which value is to be added. Elements at and above the inserted element are shifted up by one. The last element in the array is discarded.

The following code illustrates the use of the InsertIntoArray and RemoveFromArray methods:

class CTest
{
    static void Main()
    {
        ArrayUtilities arrlib = new ArrayUtilities ( );
        string[] numbers = {"one", "two", "four", "five", "six"};

        arrlib.InsertIntoArray(numbers, "three", 2);
        foreach (string number in numbers)
        {
            Console.WriteLine(number);
        }

        Console.WriteLine( );
        arrlib.RemoveFromArray(numbers, 2);
        foreach (string number in numbers)
        {
            Console.WriteLine(number);
        }
        Console.ReadLine();
    }
}

This code displays the following:

one
two
three
four
five

one
two
four
five

The sample source code is available to download: Insert Remove Array Item Source Code

Popularity: 3% [?]