IndexOutOfRange异常,即使它在WPF范围内

Daw*_*ski -3 arrays wpf

我有一个非常有趣的问题.

String[] values = new String[3];
values = line.Split(';');
Console.Write("Val:" + values[0] + ", " + values[1] + ", " + values[2]);
Run Code Online (Sandbox Code Playgroud)

有人能告诉我为什么当它在范围内时会得到IndexOutOfRangeException吗?

编辑:例如,该行有3个扇区1;2;3.控制台写出Val: ...但在输出中我仍然得到一个IndexOutOfRangeException.

Ice*_*kle 5

因为它会line.Split(';')覆盖先前的分配new String[3].从本质上讲,你的字符串数组将与';'它找到的字符nr一样长

如果你的行是这行中的东西:"this; is a test"values将是一个长度为2的字符串数组,并且values[2]将超出范围