我试图从我的DLL中返回二维数组元素的总和,但似乎无法使其正常工作。返回的整数应该是' sum'数组元素的返回值为null。可以使用一些建议......
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Class1.arraySum(array2D);
Console.WriteLine("Sum of 2D Array of numbers ({ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 })\n");
Console.WriteLine(sum);
DLL
public static int arraySum(int[,] values)
{
int sum = values.Cast<int>().Sum();
return sum;
}
答案 0 :(得分:1)
您必须声明并填充 sum
变量
int sum = Class1.arraySum(array2D);
Console.WriteLine("Sum of 2D Array of numbers ({ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 })\n");
Console.WriteLine(sum);
或“动态”写下来:
Console.WriteLine("Sum of 2D Array of numbers ({ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 })\n");
Console.WriteLine(Class1.arraySum(array2D));