When to define static constructors and when are they implicitly called???

  • Thread starter Thread starter Bob Rock
  • Start date Start date
B

Bob Rock

Hello,

I was wondering when should static constructors be defined or are they even
required??? Also, when are they implicitly called???


Bob Rock
 
static constructor is used if you need to initialize static member
variables.
If you specify the BeforeFieldInit attribute, CLR invokes the static
constructor at the first access of a static variable.
 
A static constructor should be implemented whenever you have any static
properties
defined in the class, to initialize those properties.
It is cleaner to do the initialization in the static constructor than
to directly initialize where declared if you ask me.

The static constructor runs when you create an instance of the class where
it is declared
or use any static methods declared in the class.
It runs only if there is not any other instance of the assembly currently
running,
because then it has already run.
 
Xian said:
static constructor is used if you need to initialize static member
variables.

Well, I see it as being useful in two different ways:

1) You need to initialize static variables in a more complex way than
the normal

static int foo = ...;

easily allows.

2) You want to prevent the BeforeFieldInit flag from being added to the
class (see below)
If you specify the BeforeFieldInit attribute, CLR invokes the static
constructor at the first access of a static variable.

Not quite - it does it "at some time before" the first access of a
static variable.

Note that BeforeFieldInit is added automatically by C# whenever there
*isn't* a static constructor.

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
information.
 
Dennis Myrén said:
A static constructor should be implemented whenever you have any static
properties defined in the class, to initialize those properties.
It is cleaner to do the initialization in the static constructor than
to directly initialize where declared if you ask me.

Bear in mind that it can have a significant impact on performance to
have a static constructor, as it prevents the compiler adding the
beforefieldinit flag (in C#, anyway).

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
information.

I also disagree with the readability aspect, personally - I find that
unless two members need to be initialised in some inter-related way,
it's easier to read if you put the declaration and initial assignment
together. Just a matter of personal taste though, I guess.
The static constructor runs when you create an instance of the class where
it is declared or use any static methods declared in the class.
It runs only if there is not any other instance of the assembly currently
running, because then it has already run.

No, if there are other instances of the same actual assembly, each of
them will have a different Type instance for the type, and the static
constructor will run once for each of those Type instances. It's rare
to have the same assembly loaded twice in the same AppDomain, of
course.
 
The static constructor runs when you create an instance of the class
where
No, if there are other instances of the same actual assembly, each of
them will have a different Type instance for the type, and the static
constructor will run once for each of those Type instances. It's rare
to have the same assembly loaded twice in the same AppDomain, of
course.

How could you have more instances of the same actual assembly???

Bob Rock
 
The static constructor runs when you create an instance of the class where
it is declared
or use any static methods declared in the class.
It runs only if there is not any other instance of the assembly currently
running,
because then it has already run.

What if you have static methods but no static constructor???

Bob Rock
 
Bob said:
What if you have static methods but no static constructor???

Bob Rock

Then you have no opportunity to initialize any static variables in a
common location. If your static methods only use local variables then
there isn't any need for a static constructor.
--
Tom Porterfield
MS-MVP MCE
http://support.telop.org

Please post all follow-ups to the newsgroup only.
 
Bob Rock said:
What if you have static methods but no static constructor???

What about it? The static initializer (if you have one due to static
member assignments) will be run at some stage before the first access
to a static field, as beforefieldinit will be set (assuming you're
using C# - I don't know about VB.NET).
 
Bob Rock said:
How could you have more instances of the same actual assembly???

By manually loading it. I can't remember how much work the framework
does to try to avoid it happening, but if nothing else I suspect you
could do it using the Assembly.Load* method which takes a byte array.
 
Back
Top