Problem: Field initializer cannot reference a non-static field, method, or property problem
The reason for the following code error is why the fields defined in the class are not available.
public class Test
{
Public Test ()
{
}
public int age=23;
public int temp = age;//error field initializer cannot reference a non-static field, method, or property
}
C # stipulates that only attributes or variables can be defined within a class and initialized and cannot be referenced directly by variables.
The field was called before initializing the class instance
The initialization of a field in C # is preceded by a constructor.
String y = "asdf" + x; is actually y = "asdf" + this.x;
Because this is not yet initialized, the compilation will have an error.
The property memory and variable memory allocations are not the same time.
Attributes are allocated when they are used, and variables are allocated at the time of class initialization.