Take the following C# method:
static double[] AddArrays(double[] left, double[] right)
{
if (left.Length != right.Length) {
throw new ArgumentException("Arrays to add are not the same length");
}
double[] result = new double[left.Length];
for (int i = 0; i < left.Length; i++) {
result[i] = left[i] + right[i];
}
return result;
}
As I understand it, the CLR will initialize result
to all zeros, even though AddArrays
is just about to completely initialize it anyway. Is there any way to avoid this extra work? Even if it means using unsafe C#, C++/CLI, or raw IL code?
EDIT: Can't be done, for the reasons described here.