A static constructor is used to initialize any
Static data, or to perform a participates action that needs has med once only. It is called automatically before the first instance is created or any static members are referenced.
ClassSimpleclass {// Static ConstructorStaticSimpleclass (){//...}}
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
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.
-
A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
-
Static constructors are also useful when creating Wrapper Classes for unmanaged code, when the constructor can callLoadlibraryMethod.
Example
In this example, the classBusHas a static constructor and one static member,Drive (). When
Drive ()Is called, the static constructor is invoked to initialize the class.
Public class bus { // static constructor: static bus () {system. console. writeline ( "the static constructor invoked. ") ;}< span style =" color: Blue "> Public static void drive () {system. console. writeline ( "the drive method invoked. ") ;}}< span style =" color: Blue "> class testbus { static void main () {bus. drive () ;}< SPAN class = "lw_collapsiblearea_title"> output
the static constructor invoked.
the drive method invoked.
Ref:
http://msdn.microsoft.com/en-us/library/k9x6w0hc (vs.80 ). aspx