473,508 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static constructors

what are static constructors good for? I know how they work but I cannot
imagine a scenario where one would really need them.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #1
3 1032
codymanix,

They are good for initializing values that you don't know at compile
time, but you know at runtime, and you know they will not change, or rather,
you know you only have to do it once. For example, say you want to load
configuration information for the currently running assembly. You can have
a static constructor load that information and then have it accessible
throughout the lifetime of the app domain.

Another example is something like say, the startup time of a computer.
You can load that once in the static constructor, and never have to query
for it again (because once the computer shuts down, your program won't be
running anyways).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"codymanix" <do*********************@gmx.de> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
what are static constructors good for? I know how they work but I cannot
imagine a scenario where one would really need them.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Nov 15 '05 #2
KJO
you have static data in your class which you want initialized the very first
time someone tries to reference
one.
"codymanix" <do*********************@gmx.de> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
what are static constructors good for? I know how they work but I cannot
imagine a scenario where one would really need them.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Nov 15 '05 #3

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OY**************@TK2MSFTNGP09.phx.gbl...
codymanix,

They are good for initializing values that you don't know at compile
time, but you know at runtime, and you know they will not change, or rather, you know you only have to do it once. For example, say you want to load
configuration information for the currently running assembly. You can have a static constructor load that information and then have it accessible
throughout the lifetime of the app domain.

Another example is something like say, the startup time of a computer.
You can load that once in the static constructor, and never have to query
for it again (because once the computer shuts down, your program won't be
running anyways).


More often, I have a data structure whose contents I *do* know at compile
time, but which requires running code to initialize. For example, a
Hashtable.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3636
by: javac | last post by:
for better or worse, my free time is consumed by two Java series books from Sun: Java Platform Performance, 2000, by Wilson and Kesselman Effective Java, 2001, by Bloch 1.) opinions on these...
3
18802
by: Amit | last post by:
is there anything like static constructors or destructors in C++ ? if yes, how to implement it? Thanks, Amit.
7
17175
by: mdc | last post by:
Hi, Is there any way to implement an interface as a static method in C#? If not, is this a bug? Micheal.
9
2415
by: A J Le Couteur Bisson | last post by:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing something wrong? If this is indeed the case...
6
5885
by: ~~~ .NET Ed ~~~ | last post by:
Yes, I think so at least... In C# you *can* have static properties which are quite useful when used properly. Now imagine the scenario where you need the ability (sp?) to implement a variety of...
17
2330
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
12
3408
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
3
1391
by: Jordan S. | last post by:
Just learning OOP here... Does it make sense to have a constructor in a static class? Say I have a class with a constructor and then mark that class as static; will it (the default constructor) get...
3
4346
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...
2
1992
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not...
0
7120
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...
0
7323
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,...
1
7039
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
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
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,...
0
3192
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...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
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...

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.