Export (0) Print
Expand All
Expand Minimize

Compiler Error CS0026

Keyword this is not valid in a static property, static method, or static field initializer

The this keyword is not allowed in static methods; you must have an instance before you can refer to an object with this.

The following sample generates CS0026:

// CS0026.cs
public class a
{
   public static int i = 0;

   public static void Main()
   {
      this.i = this.i + 1;   // CS0026
      // try the following line instead
      // i = i + 1;
   }
}
Show:
© 2014 Microsoft