I occasionally find it annoying that I cannot specify at which point in a class’s constructor I wish to invoke the base constructor. Having C# always invoke it before any of the code in my descendant constructor is executed sometimes causes me problems.
Considering .NET is capable of calling the ancestor constructor at any point I wondered why C# wont allow it. I contacted Anders Hejlsberg and he was kind enough to reply, unfortunately he seems to have answered a question I didn’t ask 🙂
Anyway, I have been deleting some old emails today and I came across his response, which he gave me permission to publish:
The problem with Delphi’s model (allowing constructors to be called on
an already constructed object) is that it makes it impossible to have
provably immutable objects. Immutability is an important concept because
it allows applications to hand objects to an external party without
first copying those objects and still have a guarantee that the objects
won’t be modified. If constructors can be called on already constructed
objects it obviously isn’t possible to make such guarantees. In Delphi’s
case that may be ok since Delphi doesn’t really make type safety
guarantees anyway (you can cast any object reference to a
pointer-to-something and start poking away), but .NET goes further with
type safety and this would be a big hole.
There, now get out of my head 😉