Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Cannot access a nonstatic member of outer type 'type1' via nested type 'type2'
A field in a class is not automatically available to a nested class. To be available to a nested class, the field must be static. Otherwise, you must create an instance of the outer class. For more information, see Nested Types.
The following sample generates CS0038:
// CS0038.cs
class OuterClass
{
public int count;
// Try the following line instead.
// public static int count;
class InnerClass
{
void Func()
{
// or, create an instance
// OuterClass class_inst = new OuterClass();
// int count2 = class_inst.count;
int count2 = count; // CS0038
}
}
public static void Main()
{
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.