
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Inserting and Removing Items from an Array
|
303
The RemoveFromArray method accepts two parameters. 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 ele-
ment 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);
}
}
}
This code displays the following:
one
two
three
four
five
one
two
four
five
See Also
See the “Array ...