By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hello,

I was wondering when should static constructors be defined or are they even
required??? Also, when are they implicitly called???
Bob Rock
Jul 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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.

"Bob Rock" <no***************************@hotmail.com> ha scritto nel
messaggio news:O5**************@tk2msftngp13.phx.gbl...
Hello,

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

Jul 21 '05 #2

P: n/a
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.

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...
Hello,

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

Jul 21 '05 #3

P: n/a
Xian <x.ian@_TOGLIERE_libero.it> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

P: n/a
Dennis Myrén <de****@oslokb.no> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

P: n/a
>> 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.


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

Bob Rock
Jul 21 '05 #6

P: n/a
> 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
Jul 21 '05 #7

P: n/a
Bob Rock wrote:
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


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.
Jul 21 '05 #8

P: n/a
Bob Rock <no***************************@hotmail.com> wrote:
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???


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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9

P: n/a
Bob Rock <no***************************@hotmail.com> wrote:
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???


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.