static constructors

  • Thread starter Thread starter codymanix
  • Start date Start date
C

codymanix

what are static constructors good for? I know how they work but I cannot
imagine a scenario where one would really need them.
 
codymanix,

They are good for initializing values that you don't know at compile
time, but you know at runtime, and you know they will not change, or rather,
you know you only have to do it once. For example, say you want to load
configuration information for the currently running assembly. You can have
a static constructor load that information and then have it accessible
throughout the lifetime of the app domain.

Another example is something like say, the startup time of a computer.
You can load that once in the static constructor, and never have to query
for it again (because once the computer shuts down, your program won't be
running anyways).

Hope this helps.
 
you have static data in your class which you want initialized the very first
time someone tries to reference
one.
 
Nicholas Paldino said:
codymanix,

They are good for initializing values that you don't know at compile
time, but you know at runtime, and you know they will not change, or rather,
you know you only have to do it once. For example, say you want to load
configuration information for the currently running assembly. You can have
a static constructor load that information and then have it accessible
throughout the lifetime of the app domain.

Another example is something like say, the startup time of a computer.
You can load that once in the static constructor, and never have to query
for it again (because once the computer shuts down, your program won't be
running anyways).

More often, I have a data structure whose contents I *do* know at compile
time, but which requires running code to initialize. For example, a
Hashtable.
 
Back
Top