473,788 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3153
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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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@_TOGLIER E_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.co m>
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.co m>
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.co m>
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.co m>
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
3355
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 near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
9
1111
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
2257
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 statement. To test this, I added messageboxes in all constructors indicating which constructor is called. There are four constructors which could be called: 1. Base class empty constructor 2. Base class 2-Par. constructor
9
5805
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 ValueStruct { double x ; double y ; ValueStruct() { x = y = double(0) ; } // error * } ;
4
1577
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 instance of the class or calling a static field of this class ? I need to "register" some classes in a list of classes so that I can create them without knowing them.
3
4360
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 globals are initialized and the second thread does not throw an exception. Thanks in advance. Adam Smith
15
2176
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 (a_obj) of a class type (A), then we need to define A as well, but if B has a pointer to A, then we only need to forward declare A. I was told this is because the compiler needs to see the implemenation of A when allocating memory for a_obj. ...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.