I’ve always wondered this. In C#, when do instance variables with default values get set – before or after the constructor?
For this code:
public class Foo
{
public int bar=1;
public Foo()
{
bar = 2;
}
}
...
Console.WriteLine(new Foo().bar);
Would it output 1 or 2?
The instance during construction will run through all declarations first then proceed into the constructor itself.
Thus, bar = 1 will execute before bar = 2 but the end result will still be bar = 2.