This section contains technical C# programming interview questions on constructor and destructor with answers and explanation asked in technical interviews for freshers and experienced.
Topic – Constructor and Destructor, Static Constructor in C#, Private constructors etc.
Q- What is output of the following program? When we create 2 objects of the class A e.g.
A obj1 = new A();
A obj2 = new A();
static A()//Static constructor
public A()// Constructor
Explanation: If we have static constructor in a class, it will be called before class constructor when we create first object of the class.
Static constructor will be called only once i.e. on first object creation and will not be called even we create multiple objects afterwards.
That’s the reason, in the output Static Constructor is called on first object creation.