My question is: is it not bad in the First () constructor that we send a link to the First () class BEFORE it is completely built?
Some. This can be a problem, of course.
If the Second constructor just keeps a reference for later use, it's not so bad. If, on the other hand, the Second constructor accesses First :
public Second(First f) { f.DoSomethingUsingState(); }
... and the condition is not yet configured, then this, of course, will be a very bad thing. If you call the virtual method on First , then it can be even worse - you can end up calling code that did not even have the ability to run any of its constructor body (although its variable initializers will be executed).
In particular, readonly fields are visible first with one value and then with another ...
I wrote about this some time ago, which may provide additional information.
Of course, without doing this kind of thing, itโs quite difficult to create two mutually referenced immutable objects ...
Jon Skeet Aug 02 2018-12-12T00: 00Z
source share