Today, I wanted to understand how to call a base class constructor AFTER the derived class constructor block had been executed. As it turned out I was mistaken in thinking this was possible, but it was a useful learning experience.
Take the following code:
class MyBaseClass
{
public MyBaseClass(int i)
{
}
}
Assume that i would like to derive from this class, while invoking the base class constructor:
class MyDerivedClass1 : MyBaseClass
{
public MyDerivedClass1(int i) : base(i)
{
}
}
As it turns out, in C# this is not possible as it introduces an inverse dependency into the mix (in this instance making the base class depend on the derived class).
See the following article:
http://blogs.msdn.com/ericlippert/archive/2010/01/28/calling-constructors-in-arbitrary-places.aspx
And, right enough, your C# code will not compile:
class MyDerivedClass2 : MyBaseClass
{
public MyDerivedClass2(int i)
{
}
}
‘TestBaseClassCall.MyBaseClass’ does not contain a constructor that takes ’0′ arguments
no comment untill now