Compiler Error CS0038

 

Updated: July 20, 2015

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

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

Show: