Skip to main content

How to Resolve Error "CS0122: 'member' is inaccessible due to its protection level" in C#

The Compiler Error CS0122 is an Access Control error. The message reads: "'MyClass.MyMember' is inaccessible due to its protection level".

C# uses Access Modifiers (keywords like public, private, internal, protected) to enforce encapsulation. This error occurs when you attempt to use a variable, method, or class from a location that is forbidden by these security rules. The most common cause is forgetting that class members are private by default if no modifier is specified.

This guide explains the rules of visibility and how to expose your members correctly.

Understanding Default Access Modifiers

In C#, if you do not explicitly type an access modifier, the compiler assigns a default one. This is the source of 90% of CS0122 errors.

  • Members (Methods, Fields, Properties) inside a class are private by default.
  • Top-level Types (Classes, Structs, Interfaces) are internal by default.
note

Quick Rule: private means "Only this class can touch it." internal means "Only this project (.csproj) can touch it." public means "Everyone can touch it."

Scenario 1: Private Members (The Default Trap)

This scenario usually happens when a developer defines a class to hold data but forgets to make the fields public.

Example of error

public class User
{
// No modifier specified -> Defaults to 'private'
string Name;

// Explicitly private
private int Age;
}

public class Program
{
static void Main()
{
var u = new User();

// ⛔️ Error CS0122: 'User.Name' is inaccessible due to its protection level
// Even though we are in the same file or project, 'Name' belongs ONLY to 'User'.
u.Name = "Alice";
}
}

Solution: Add 'public'

If you want other classes to read or write the data, you must explicitly mark it as public (or internal).

public class User
{
// ✅ Correct: Explicitly public
public string Name;

// Better practice: Use Properties
public int Age { get; set; }
}

public class Program
{
static void Main()
{
var u = new User();

// ✅ Correct: Access allowed
u.Name = "Alice";
}
}

Scenario 2: Protected Members (Inheritance vs. Instances)

The protected modifier implies: "Visible to this class AND any class that inherits from it."

However, beginners often confuse this with public access. protected allows a child class to access the member internally, but it does not allow an outside user (like Main) to access that member on an instance of the child class.

Example of error

public class Parent
{
protected void FamilySecret() { }
}

public class Child : Parent
{
// Child inherits FamilySecret...
}

public class Program
{
static void Main()
{
Child c = new Child();

// ⛔️ Error CS0122: 'Parent.FamilySecret' is inaccessible...
// Main is not part of the 'Parent' family tree.
// It cannot access protected members from the outside.
c.FamilySecret();
}
}

Solution

If Main needs to call it, it must be public. If only the child needs it, keep it protected but call it from inside the child.

public class Child : Parent
{
public void RevealSecret()
{
// ✅ Correct: 'this' allows access to protected members inside the family
this.FamilySecret();
}
}

Scenario 3: Internal Classes (Cross-Project Issues)

If you have a Solution with two projects (e.g., MyApp.Main and MyApp.Library), you might get CS0122 when trying to use a class from the library.

Example of error

File in Project B (MyApp.Library):

// No modifier -> Defaults to 'internal'
class Calculator
{
public int Add(int a, int b) => a + b;
}

File in Project A (MyApp.Main):

using MyApp.Library;

public class Runner
{
void Run()
{
// ⛔️ Error CS0122: 'Calculator' is inaccessible...
// Project A cannot see 'internal' classes inside Project B.
var calc = new Calculator();
}
}

Solution: Make the Class Public

Update the library code to expose the class.

namespace MyApp.Library
{
// ✅ Correct: Now visible to other projects referencing this DLL
public class Calculator
{
public int Add(int a, int b) => a + b;
}
}
tip

InternalsVisibleTo: If you strictly want to keep the class internal but allow a specific project (like a Unit Test project) to see it, you can add [assembly: InternalsVisibleTo("MyTestProject")] in your AssemblyInfo.cs or project file.

Conclusion

CS0122 is the compiler enforcing your security settings.

  1. Check Defaults: If a member has no modifier, it is private. If a class has no modifier, it is internal.
  2. Expose API: Use public for methods and properties you want the outside world to use.
  3. Check Inheritance: Remember that protected is for subclasses, not for instances.