The Type property of Array objects provides information about array type declarations. Array objects with the same array type share the same Type object, so the array is said to be homogenous. But you can store wrapper classes as objects with aggregation or composition, which can store or link references to other objects. Listing 20.9 shows conversion of an Int32 array to a double array by copying and then modifying elements with changed values.
Listing 20.9: Array Copy and Conversion
// copying and converting array elements
using System;
class Test
{
public static void TestForEach(ref int[] myArray)
{
foreach (int x in myArray)
{
Console.WriteLine(x);
}
}
public static void TestForEach(ref double[] myArray)
{
foreach (double x in myArray)
{
Console.WriteLine(x);
}
}
public static void Main()
{
// an int array and an Object array
int[] myIntArray = new int[5] { 5, 4, 3, 2, 1 };
TestForEach(ref myIntArray);
Console.WriteLine("myIntArray: Type is {0}", myIntArray.GetType());
double[] myDblArray = new double[5];
Array.Copy(myIntArray, myDblArray, myIntArray.Length);
for (int i = 0; i < myDblArray.Length; i++)
myDblArray[i] += myDblArray[i] / 19;
TestForEach(ref myDblArray);
Console.ReadLine();
}
}
Output of above listing:

Conclusion
Hope this article would have helped you in understanding the Array Conversions in C#. See other articles on the website on .NET and C#.


David BozjakPosted Feb 18, 2010, 3:07 AM
ref keyword in the methods is unnecessary, and should be removed. Even if you weren't just printing values, arrays are already sent by reference, so I can't really see the reason behind ref keyword. You should only use ref keyword when you want the reference in the calling function to change. In your example, the only reason you would want to use a ref keyword is when you want the myIntArray in main to change to null after calling a method. I suggest you read this excellent article about parameter passing: http://www.yoda.arachsys.com/csharp/parameters.html by Jon Skeet