Yes, a class can have static constructor. Static constructors are called before when any static fields are executed. These are used to initialize static class members and called automatically before the first instance is created or any static members are referenced. This is also known as before instance constructor. Below is one example of above described theory.
static int i;
i = 10;
Console.WriteLine("In Static Constructor");
Console.WriteLine("In Instance Constructor");
public static void Main()
Sample s = new Sample();
Asked In: Many Interviews |