我想增加(使用计数器)C#数组的值。但是,我总是收到一个错误:数组中的增量计数器C#
索引超出了数组的范围。
这是我的代码。
while ((line = s.ReadLine()) != null)
{
string[] parts = new string[40];
parts=line.Split(' ');
int a;
for (a = 0; a <= (parts.Length - 1); a++)
{
if (parts[a] == "if")
{
node = node + 1;
edge = edge + 1;
int b = a + 2;
Console.Write(parts[b]);
if ((parts[a + 2]) == "{")
{
node = node + 1;
}
}
}
}
在哪条线路?你调试了你的代码吗? http://ericlippert.com/2014/03/05/how-to-debug-small-programs/您可以通过调试轻松找到问题所在。 –
它在这条线上吗? 'if((parts [a + 2])==“{”)'? – JoJo
'new string [40]'是浪费时间和内存,因为下一个语句会分配一个新值。只需使用'var part = line.Split('');' –