我试图制作一个程序,它总结了数组中的元素.但我在MVS上有' System.IndexOutOfRangeException '错误.谁能说出我的错误在哪里?
public static int Sum(int[,] arr)
{
int total = 0;
for (int i = 0; i <= arr.Length; i++)
{
for (int j = 0; j <= arr.Length; j++)
{
total += arr[i,j];
}
}
return total;
}
static void Main(string[] args)
{
int[,] arr = { { 1, 3 }, { 0, -11 } };
int total = Sum(arr);
Console.WriteLine(total);
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)