When using easy syntax to initialize derived class fields by directly assigning values to them, compiler emits an IL code which first initializes derived class fields and then call base .ctor. Why it doesnt call base .ctor fields and initialize the derived class fields. What if I would have fields which depend on base class's fields?
I would love to put some code sample here but I am posting this question from my mobile phone.
I think it is because it needs to handle the case where a virtual method or intializer is called within the constructor.
Eric Lippert has a two part series on his blog.
http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx
Just mentioning here for your convineance. Hope this helps.
Calling methods on derived types from constructors is dirty pool, but it is not illegal.
And stuffing not-quite-constructed objects into global state is risky, but not illegal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With