Compiler Error CS0026
Keyword 'this' is not valid in a static property, static method, or static field initializer
The this keyword refers to an object, which is an instance of a type. Since static methods are independent of any instance of the containing class, the "this" keyword is meaningless and is therefore not allowed. For more information, see Static Classes and Static Class Members and Objects.
Example
The following example generates CS0026:
// CS0026.cs
public class A
{
public static int i = 0;
public static void Main()
{
// CS0026
this.i = this.i + 1;
// Try the following line instead:
// i = i + 1;
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for