I have the following scenario:
Class A
{
public static A instance;
static A()
{
if(condition)
{
instance = new B();
}
else
{
instance = new A();
}
}
public A()
{
WriteSomething();
}
virtual void WriteSomething()
{
Console.WriteLine("A constructor called");
}
}
Class B : A
{
public B()
{
WriteSomething();
}
override void WriteSomething()
{
Console.WriteLine("B constructor called");
}
}
The problem is that when A.instance is called the first time and if condition is true and the B() constructor is called, for some reasons I do not undestand the output of the program is "A constructor called".
Can you please help with an explanation!
Thank you!
conditionvalue? Could you also put the calling code into the question so that we can see everything you're doing?