Skip to main content

How to Resolve Error "CS0038: Cannot access a nonstatic member of outer type 'type1' via nested type 'type2'" in C#

The Compiler Error CS0038 is a scoping error related to Nested Classes. For example, the message could be: "Cannot access a nonstatic member of outer type 'Outer' via nested type 'Inner'".

This error occurs because, unlike in some languages (like Java), a nested class in C# does not automatically possess a reference to an instance of the outer class. The nested class is effectively just a type defined inside another namespace; it does not "live" inside a specific object of the outer class implicitly. Therefore, it cannot access the outer class's instance variables (non-static fields) without an explicit reference.

This guide explains the relationship between outer and inner classes in C# and how to pass the necessary references to fix this error.

Understanding Nested Class Scope

In C#, nesting ClassB inside ClassA is primarily a way to organize code and control visibility (e.g., making ClassB private so only ClassA can use it).

  • It is NOT an inheritance relationship: ClassB does not inherit from ClassA.
  • It is NOT an implicit instance relationship: Creating an instance of ClassB does not automatically link it to a specific instance of ClassA.

Because they are unlinked, ClassB cannot simply say "Give me ClassA.MyVariable" because the compiler asks: "Which instance of ClassA are you talking about?"

Scenario 1: Direct Access to Instance Fields

The most common cause is trying to access a variable defined in the parent class from a method in the child class, assuming the child class "knows" about the parent.

Example of Mistake

public class Outer
{
// An instance field (non-static)
public int OuterValue = 42;

public class Inner
{
public void PrintValue()
{
// ⛔️ Error CS0038: Cannot access a nonstatic member of outer type 'Outer'
// via nested type 'Inner'.
// The 'Inner' class doesn't know which 'Outer' object owns 'OuterValue'.
System.Console.WriteLine(OuterValue);
}
}
}

Solution 1: Pass the Outer Instance Explicitly

If the Inner class needs to work with data from the Outer class, you must explicitly give it an instance of Outer. You can do this via a constructor or a method parameter.

Approach A: Constructor Injection

This links the inner object to the outer object at the moment of creation.

public class Outer
{
public int OuterValue = 42;

public class Inner
{
private Outer _parent;

// ✅ Correct: Require an instance of Outer to create Inner
public Inner(Outer parent)
{
_parent = parent;
}

public void PrintValue()
{
// ✅ Correct: Access the field through the explicit reference
System.Console.WriteLine(_parent.OuterValue);
}
}

public void Run()
{
// Pass 'this' (the current Outer instance) to the Inner class
Inner i = new Inner(this);
i.PrintValue();
}
}

Approach B: Method Parameter

If you don't need a permanent link, just pass the outer instance to the specific method that needs it.

public class Inner
{
// ✅ Correct: Pass the instance as an argument
public void PrintValue(Outer parent)
{
System.Console.WriteLine(parent.OuterValue);
}
}

Solution 2: Use Static Members

If the variable in the outer class is shared by all instances (i.e., it is static), the nested class can access it directly, because static members belong to the type, not an instance.

public class Outer
{
// Changed to 'static'
public static int GlobalValue = 100;

public class Inner
{
public void PrintValue()
{
// ✅ Correct: Nested classes have access to private/public
// STATIC members of the outer class.
System.Console.WriteLine(GlobalValue);
}
}
}
note

This solution only applies if the data should be global. Do not make a field static just to silence a compiler error if the data is supposed to be unique per object.

Conclusion

CS0038 clarifies the boundary between your nested types.

  1. Remember: Nested classes in C# are independent types. They do not implicitly hold a reference to their "parent" object.
  2. The Fix: If you need to access the outer class's data, pass an instance of the outer class (this) into the inner class's constructor or method.
  3. The Alternative: If the data is not specific to an instance, make the field static.