PC Review


Reply
Thread Tools Rate Thread

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

 
 
Bob Rock
Guest
Posts: n/a
 
      14th May 2004
Hello,

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


Bob Rock


 
Reply With Quote
 
 
 
 
Xian
Guest
Posts: n/a
 
      14th May 2004
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" <(E-Mail Removed)> ha scritto nel
messaggio news:(E-Mail Removed)...
> Hello,
>
> I was wondering when should static constructors be defined or are they

even
> required??? Also, when are they implicitly called???
>
>
> Bob Rock
>
>



 
Reply With Quote
 
Dennis Myrén
Guest
Posts: n/a
 
      14th May 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> I was wondering when should static constructors be defined or are they

even
> required??? Also, when are they implicitly called???
>
>
> Bob Rock
>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th May 2004
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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th May 2004
Dennis Myrén <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Bob Rock
Guest
Posts: n/a
 
      14th May 2004
>> 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


 
Reply With Quote
 
Bob Rock
Guest
Posts: n/a
 
      14th May 2004
> 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


 
Reply With Quote
 
Tom Porterfield
Guest
Posts: n/a
 
      14th May 2004
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.
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th May 2004
Bob Rock <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th May 2004
Bob Rock <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making sure static constructors have been called James Crosswell Microsoft C# .NET 7 6th Dec 2006 11:15 PM
CLI/C++ static constructors Mark Ingram Microsoft VC .NET 1 6th Dec 2006 09:35 AM
When to define static constructors and when are they implicitly called??? Bob Rock Microsoft C# .NET 9 14th May 2004 02:22 PM
When to define static constructors and when are they implicitly called??? Bob Rock Microsoft Dot NET Framework 9 14th May 2004 02:22 PM
BUG in static constructors?!?!? Sunny Microsoft C# .NET 7 3rd Nov 2003 10:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:00 PM.