为啥用foreach 会导致数组溢出呢,for就不会。求教
namespace projGetMaxScore
{
class Program
{
static void Main(string[] args)
{
int[] sorce = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
string[] name = new string[] { "89", "90", "98", "56", "60", "91", "93", "85" };
int Topsorce = 0;
string Name = "";
// foreach (int x in sorce)
//{
// if (sorce[x] >= Topsorce)
// Topsorce = sorce[x];
// Name = name[x];
// }
for(int i=0;i<sorce.GetLongLength(0);i++)
{
if (sorce[i] >= Topsorce)
Topsorce = sorce[i];
Name = name[i]; }
Console.Write("分数最高的是{0},分数是{1}", Name, Topsorce);
}
}
}