473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
9 3133
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
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
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
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
>> 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
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
9
by: Bob Rock | last post by:
Hello, I was wondering when should static constructors be defined or are they even required??? Also, when are they implicitly called??? Bob Rock
1
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the...
9
by: Peter Oliphant | last post by:
I've been told that value structs can have default constructors (I'm using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code generates the following error: value struct...
4
by: hyd | last post by:
With C++/CLI - VS2005, is it possible to have static constructors that are automatically called when the owner assembly is loading ? Otherwise, how it is possible to call it without creating an...
3
by: Adam | last post by:
What happens if one thread is executing a static constructor and another thread starts. Does the second thread block until the first is done in the static constructor? I want to make sure all my...
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.