This documentation is archived and is not being maintained.

Compiler Warning (level 2) CS0251 

Error Message

Indexing an array with a negative index (array indices always start at zero)

Do not use a negative number to index into an array.

The following sample generates 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);
      }
   }
}
Show: