Let’s say I have the following, rather simple, class hierarchy:
class FooBase
{
public FooBase(int a)
{
Console.WriteLine("FooBase({0})", a);
}
}
class MyFoo
{
public MyFoo(int a, string s)
{
Console.WriteLine("MyFoo({0}, \"{1}\"", a, s);
}
}
Now, let’s say I’m adding a new constructor to MyFoo. You can use the following syntax to call another constructor with different parameters on your own type:
MyFoo()
: this(10, "something else")
{
Console.WriteLine("MyFoo()");
}
In this case, the output after constructing with this constructor would be:
MyFoo(10, "something else")
MyFoo()
You can also use the following syntax to call a base class’s constructor:
MyFoo()
: base(10)
{
Console.WriteLine("MyFoo()");
}
And in this case, the output after constructing with this constructor would be:
FooBase(10)
MyFoo()
So why can’t you call both, like so (for example):
MyFoo()
: base(10), this(15, "something else")
{
Console.WriteLine("MyFoo()");
}
? In this case, I would expect the base class’s constructor to run first, MyFoo(int, string) next, and then the body of the current constructor to run third, giving the following output:
FooBase(10)
MyFoo(15, "something else")
MyFoo()
I can’t think of any reason for why that wouldn’t be allowed. Perhaps you can do it, and I just don’t know the syntax, but when you look at articles in this topic (for example, this one on MSDN) it just lists the first two that I mentioned, without any mention of whether you can (or how to, or why you can’t) combine them.