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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...