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).

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...