This documentation is archived and is not being maintained.

Compiler Error CS0236

Visual Studio 2008

A field initializer cannot reference the nonstatic field, method, or property 'field'

Instance fields cannot be used to initialize other instance fields outside a method. If you are trying to initialize a variable outside a method, consider performing the initialization inside the class constructor. For more information, see Methods (C# Programming Guide).

The following sample generates CS0236:

// CS0236.cs
public class MyClass
{
   public int i = 5;
   public int j = i;  // CS0236
   public int k;      // initialize in constructor

   MyClass()
   {
      k = i;
   }

   public static void Main()
   {
   }
}
Show: