In the .NET framework version 2.0, Microsoft added a generic method named ConvertAll to the Array class. This method allows an entire array to be converted from one data type to another. The method creates a new array, loops through the existing array and executes a delegate for each element. The resultant value from the delegate is stored in the new array, which becomes the method's return value.
The syntax for the ConvertAll method is as follows:
result = Array.ConvertAll<input-type, output-type>(input-array, converter);
The four changeable elements in the syntax are:
-input-type. The type of the items held in the input array.
-output-type. The type of the items to be created in the returned array.
-input-array. The array to duplicate and convert. This must be a one-dimensional, zero-based array.
-converter. A delegate containing the code to use to convert each of the items in the array. This may be as simple as a cast operation or be much more complicated, perhaps converting each numeric value to its English text equivalent as a string. The delegate must accept a parameter of the input data type and return a result of the output data type.
int[] integerArray = new int[] { 1, 2, 3, 4, 5 };
long[] longArray = Array.ConvertAll<int, long>(integerArray,
delegate(int i)
{
return (long)i;
});