Looking at well known CS0236 error message
Error (CS0236): A field initializer cannot reference the nonstatic field, method, or property and the
reason for such restriction is intialization order:
You cannot use an instance variable to initialize another instance variable.
But this
is not an instance variable, is it? My question is why there is such strict requirement even for refrencing class methods? In my understanding, it would suffice to restrict just invocation of class methods, e.g.:
bool _field;
bool GetField() => _field;
bool _field2 => GetField(); // clearly an error, invoking something that might not be initialized
// why compiler cannot safely bind a delegate? It does not have to access anything, just take an address
Func<bool> Func1 = GetField; // does not have to error, refencing but not invoking
Is this just a design decision or are there any technical limitations that prevent delegate binding during field initialization?
Edit: I am interesed in the technical aspects, rather than bugfixing. What prevents the compiler binding the instance method? Address of this
must be avaialable at the point when binding happens and something like funcPtr = instanceaddress+memberoffset
does not seem to have any side effects on initialization.