Static Constructors (C# Programming Guide)
A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.
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.
-
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 call the LoadLibrary method.
Reference
Objects, Classes and Structs (C# Programming Guide)Destructors (C# Programming Guide)
Concepts
C# Programming GuideOther Resources
Constructors (C# Programming Guide)It would worthwhile if the content discusses about the static constructors for ValueType (struct) and their subtle behavior.
- 12/23/2009
- Vivek Ragunathan
- 5/4/2009
- Devashri Oza
- 5/4/2009
- Devashri Oza
Instantiating the derived class will call the base class instance constructor, therefore the base class static constructor will be executed first. But static members of the derived class can be used without triggering the base class static constructor.
- 9/13/2007
- Ben Voigt - old ID
- 7/20/2008
- Stanley Roark