Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0122 – ‘member’ is inaccessible due to its protection level

C# Compiler Error

CS0122 – ‘member’ is inaccessible due to its protection level

Reason for the Error

You will usually receive this error when you are trying to access a member of a class (Eg : Method) when the member’s access modifier prevents you from accessing it.

For example, compile the below code snippet.

namespace DeveloperPublishNamespace
{
    public class Class1
    {
        void Method1()
        {
        }
    }

    public class DeveloperPublish
    {         
        public static void Main()
        {
            Class1 obj1 = new Class1();
            // Compiler Error
            obj1.Method1();
        }
    }
}

The C# compiler will result with the below C# compiler error as the Method1 by default has the private access modifier which cannot be accessed directly.

Error CS0122 ‘Class1.Method1()’ is inaccessible due to its protection level ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 16 Active

C# Error CS0122 – 'member' is inaccessible due to its protection level

Solution

You can avoid this error by changing the access modifier of the Method to allow its access (if needed).