编译器警告(等级 2)CS0251
更新:2007 年 11 月
错误消息
用负索引对数组进行索引(数组索引总是从零开始)
不要在数组中使用负数进行索引。
下面的示例生成 CS0251:
// CS0251.cs
// compile with: /W:2
class MyClass
{
public static void Main()
{
int[] myarray = new int[] {1,2,3};
try
{
myarray[-1]++; // CS0251
// try the following line instead
// myarray[1]++;
}
catch (System.IndexOutOfRangeException e)
{
System.Console.WriteLine("{0}", e);
}
}
}