About a year back there was a thread in which I was unhappy that SetDefaults got called before my constructor code had run -- it is just wrong to make other function calls before the constructor has run. My error was failing to realize that SetDefaults IS part of the constructor. It is called from the base class constructor before returning to run the derived constructor code. Therefore there is nothing wrong with the SetDefaults call (as long as you realize it is functionally part of the constructor, and will execute prior to the code in the constructor). An interesting idiom, but nothing wrong with doing that.
What I do not understand is why it is important to put initialization code in SetDefaults instead of in the constructor. The base classes will be at least as initialized by the time the constructor code gets to run, because that comes later than the call to SetDefaults.
Some have pointed out that callers can get instances from the cache, and the constructor will not run in such a case. True, but neither will SetDefaults, since it is part of construction. What you get from the cache is an already-constructed object with public properties set to the values you specify.
As far as I can see, the question comes down to whether there is ever a time when SetDefaults will be run, other than when it is called from the base class constructor. To put it another way, is there ever a time when SetDefaults will run, but the constructor code will not? If so, when is that? If not, then why is it better to put initialization code in SetDefaults rather than in the constructor?
I want to emphasize that I am not trying to be contentious, and I am not trying to claim there is anything wrong. I just want to understand.
--EV
Comment