Skip to main content

Posts

Showing posts from September, 2015

Static Constructors

A static constructor is used to initialize any  static  data, 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. Code :-  class SimpleClass { // Static variable that must be initialized at run time.  static   readonly   long baseline; // Static constructor is called at most one time, before any  // instance constructor is invoked or member is accessed.  static SimpleClass() { baseline = DateTime.Now.Ticks; } } Static constructors have the following properties: A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to initialize the  class  before the first instance is created or any static members are referenced. A static constructor cannot be called directly. The user has no control on when the static constructor is executed in the program.