2 out of 7 rated this helpful - Rate this topic

Compiler Error CS0236 

Error Message

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

Instance fields cannot be used to initialize other instance fields outside of a method. If you are attempting to initialize a variable outside of 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()
   {
   }
}
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Another example
The following code will give you same error:

public class class1
{
public string method1()
{
return "Hello World";
}
}

public class class2
{
class1 cl = new class1();
string str = cl.method1;
}