C # variable initialization problem: the initial value of a field cannot reference non-static fields, methods, or attributes,
Http://www.cnblogs.com/bluestorm/p/3432190.html
Problem: non-static fields, methods, or attributes cannot be referenced in the field's Initial Value Setting item
The reason for the following code error is that the fields defined in the class cannot be used. Why?
Public class Test
{
Public Test ()
{
}
Public int Age = 23;
Public int temp = Age; // the initial value of the ERROR field cannot reference non-static fields, methods, or attributes.
}
C # specifies that only attributes or variables can be defined within the class and initialized. Variables cannot be referenced directly.
The field is called before the class instance is initialized.
The initialization of fields in c # is prior to the constructor.
String y = "asdf" + x; it is actually y = "asdf" + this. x;
Because this is not initialized yet, an error is reported during compilation.
The allocation time of attribute memory and variable memory is different.
The attribute is allocated when it is used, and the variable is allocated during class initialization.