C# Inheritance lesson
Today there was a pretty active thread on the Advanced DotNet mailing list entitled: "Partially constructed objects in C#"
http://discuss.develop.com/archives/wa.exe?A1=ind0306c&L=advanced-dotnet
I found had noticed this just the other day when "playing" with inheritance and was surprised coming from a C++ background. I had to try it out in C# and C++ just to make sure.
I created a BaseClass in C# and C++ with a constructor and a single virtual method f1();
The BaseClass CTOR prints an I am here message and then calls f1()
A second class is derived from BaseClass and overrides f1()
In C# the output is:
BaseClass.CTOR
DerivedClass.f1
DerivedClass.CTOR
and in C++ I get:
BaseClass.CTOR
BaseClass.f1
DerivedClass.CTOR
I suppose this shouldn't have been surprising if I had RTFB but ...
The thread is interesting. C# and Java have the same behavior. There was argument as to which is a better way, the C++ or the C#. It really doesn't matter because each language is the way it is. But I hadn't really thought about calling virtual methods from the construct as being unsafe.
Learn every day. My motto.