Compiler Error CS0236

A field initializer cannot reference the non-static field, method, or property 'name'.

Instance fields cannot be used to initialize other instance fields outside a method.

To correct this error

If you are trying to initialize a variable outside a method, consider performing the initialization inside the class constructor. For more information, see Methods.


The following sample generates CS0236, and shows how to fix it:

public class MyClass
    public int i = 5;

    // To fix the error, remove "= i", and uncomment the line in constructor.
    public int j = i;  // CS0236

    public MyClass()
        // Uncomment the following.
        //j = i;