I have a basic query. I have a derived class which is empty. I notice that when I run the below pasted piece of code (runs in Linqpad), I end up with 4 objects of the BASE Class. My understanding is when we instantiate a derived class object , in absence of its own constructor, BASE Class constructor would be invoked , resulting in 1 instance of the derived class.
void Main()
{
NestedS n1 = new NestedS();
NestedS n2 = new NestedS();
NestedS n3 = new NestedS();
}
public class Base1
{
private static readonly Base1 instance = new Base1();
private static int numInstance = 0;
private readonly object lock1 = new object();
public Base1()
{
lock(lock1)
{
numInstance.Dump("before");
numInstance++;
numInstance.Dump("here");
}
}
}
public class NestedS : Base1{
}
The code results in 4 instances of the derived class. Can someone explain to me the reason behind the same.
Update: Sorry , rephrasing my query here, pasting output as well. From output , numInstance should have been initialized to 1 already before second instance is created, but it is still 0 at time of second instance creation :
before
0
here
1
before
0
here
1
before
1
here
2
before
2
here
3 ```