C# Error CS0027 – Keyword ‘this’ is not available in the current context

C# Compiler Error Message

CS0027 Keyword ‘this’ is not available in the current context

Reason for the Error

You will receive this error if you use this keyword outside of a property , function or constructor. For example, try compiling the below code snippet.

using System;
public class DeveloperPublish
{
    public int result = this.Function1(); 
    public int Function1()
    {
        return 1;
    }

    public static void Main()
    {
        Console.WriteLine("Main");
    }
}

You will receive the error “CS0027 Keyword ‘this’ is not available in the current context” because this.Function1() is used in a wrong place.

C# Error CS0026 - CS0027 Keyword 'this' is not available in the current context

Solution

To resolve this error, you’ll need to modify your code so that use of this keyword is moved inside a property, function or even a constructor.

Copy link
Powered by Social Snap