There are three issues - two of which are typos I think.
Typos
Console.Writeline("Four");
should be Console.WriteLine("Four");
new b().whatAmI
, the field whatAmI
is private so inaccessible, I think you meant to call the property's getter:
new b().WhatAmI
The Bug
internal a()
{
Console.WriteLine("seven and {0}", new b().WhatAmI);
}
As b
is derrived from a
, the call to b
's constructor (new b()
) actually calls a
's constructor, which in turn calls b
's again, which ...
The result is a StackOverflowException
.
Example without smoke and mirrors
class Program
{
static void Main(string[] args)
{
new C();
}
public class A
{
public A()
{
new B();
}
}
public class B : A
{
}
public class C : A
{
}
}