What is Static Constructor ?


A static constructor is used to initialize any static members, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
Example

class Date
{
    // Static variable that must be initialized at run time. 
    static readonly long currentDate;

    // Static constructor is called at most one time, before any 
    // instance constructor is invoked or member is accessed. 
    static Date()
    {
        currentDate= DateTime.Now;
    }
}

Note :

  • A static constructor does not take access modifiers or any┬áparameters.
  • A static constructor can not be called directly.
  • The user has no control on static members when the static constructor is executed in the program.