I've looked around for a definitive answer to this question and can't seem to find one. If someone could point me in the right-direction in terms of documentation, it would be greatly appreciated.
Now I fully understand the order of initialization of an object when it comes to construction, fields and inheritance etc, but can someone tell me the order of initialization of the following object:
public class foo
{
private ECPoint point = new ECPoint() {
X = MethodCall(),
Y = MethodCall()
}
}
Now in the above example, let's say that MethodCall() is reading from somewhere like a MemoryStream(). So each of the above two function calls has a side-effect of advancing the stream pointer. My question is: Is it GUARANTEED that X will be read and initialized before Y?
The reason I ask this question is because in C++ for example, if you have functions as arguments to a method, you can't guarantee the order in which those arguments are evaluated. The ONLY guarantee you have is that they WILL be fully evaluated before the function is called. Is it the same here? In the above, are they initialized in order, or is the just a guarantee that X and Y will be fully evaluated before the construction of ECPoint?
The need for explicitly expressed guarantee can extended to dependent setters.
internal class LockingProperty
{
private Int32 value;
public int Value
{
get => value;
set
{
if ( LockValue )
return;
this.value = value;
}
}
public bool LockValue { get; init; }
}
Value can be set only if LockValue allows for it.
var obj = new LockingProperty() { LockValue = true, Value = 10, };
Console.WriteLine ( obj.Value ); // 0
obj = new LockingProperty() { Value = 10, LockValue = true, };
Console.WriteLine ( obj.Value ); // 10
While textual order obviously work as expected even in optimalized compilation, w/o explicit gurantee code like this is a risk, mostly regarding to new features added to language.