Compiler Error CS0122

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

'member' is inaccessible due to its protection level

The access modifier for a class member prevents accessing the member. For more information, see Access Modifiers.

One cause of this (not shown in the sample below) can be omitting the /out compiler flag on the target of a friend assembly. For more information, see Friend Assemblies and /out (C# Compiler Options)

The following sample generates CS0122:

// CS0122.cs  
public class MyClass  
{  
    // Make public to resolve CS0122  
    void MyMethod()  
    {  
    }  
}  
  
public class MyClass2  
{  
    public static int Main()  
    {  
        MyClass a = new MyClass();  
        // MyMethod is private  
        a.MyMethod();   // CS0122  
        return 0;  
   }  
}  

Show: