Edit

Share via


Compiler Error CS0038

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()
    {
    }
}