Static constructors in C# will be the death of me
-
Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:
cheers, Chris Maunder
CodeProject.com : C++ MVP
Are you sure ? I just did some tests, and it seems to always do option (b)
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:
cheers, Chris Maunder
CodeProject.com : C++ MVP
-
-
Are you sure ? I just did some tests, and it seems to always do option (b)
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
To get the definitive answer when static ctor (or type initializers) do run here is the blog about it: http://geekswithblogs.net/akraus1/articles/90803.aspx Static ctors are thread safe they are normally called before any static method or field is accessed. This "normally" rule is broken in certain cases to ensure that the initialization sequence does not end up in recursion or a deadlock. Chris I supose you do have a class with statics and circular dependencies if you do not see your static ctor run before first access to your class. This is bad design ;P and should be avoided in favor of a more explicit initialization semantics like the double checked lock pattern. Yours, Alois Kraus
-
Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:
cheers, Chris Maunder
CodeProject.com : C++ MVP
Now, now...Let's not go getting angry. I think there are 2 reasons. First off, they function similarly to ObjectMain in COM, so they were kinda copying that pattern. Second, and more importantly - what order would you like your objects to be called in? You could open a whole rats nest by having various static constructors referencing static members in all your different classes. That's a messy situation that's best left avoided. J
-
I just did a test, the static constructor is called just before a static variable is accessed, or a static method is called. So, just in time, which seems fine to me.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Desktop or ASP.NET? In ASP.NET I'm running through the code again and again and the static constructor isn't being called until an object is being instantiated, even if I call a static method. Regardless: I will hark back to my C++ days and Trust No One. Explicit calls are now the order of the day.
cheers, Chris Maunder
CodeProject.com : C++ MVP
-
Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:
cheers, Chris Maunder
CodeProject.com : C++ MVP
If you ask me, you're just being unmanageable. :)
Author of The Career Programmer and Unite the Tribes www.PracticalStrategyConsulting.com
-
Desktop or ASP.NET? In ASP.NET I'm running through the code again and again and the static constructor isn't being called until an object is being instantiated, even if I call a static method. Regardless: I will hark back to my C++ days and Trust No One. Explicit calls are now the order of the day.
cheers, Chris Maunder
CodeProject.com : C++ MVP
Chris Maunder wrote:
In ASP.NET I'm running through the code again and again and the static constructor isn't being called until an object is being instantiated, even if I call a static method
Did you see someone else replied about this ? It works fine for me with a basic test scenario, even in ASP.NET. So, I guess you've run into the specific problem the other poster mentioned.
Chris Maunder wrote:
Regardless: I will hark back to my C++ days and Trust No One.
I trust C++ more than I do C#.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Desktop or ASP.NET? In ASP.NET I'm running through the code again and again and the static constructor isn't being called until an object is being instantiated, even if I call a static method. Regardless: I will hark back to my C++ days and Trust No One. Explicit calls are now the order of the day.
cheers, Chris Maunder
CodeProject.com : C++ MVP
Hi Chris, sorry to hear you cursing against C#. You are right that there is no way to initialize statics during application startup. But this was never the case even in C++ days. In dlls there is an initialization chain which ensures that during module load the statics are intialized correctly. Things are different with JITed languages since most of the code has not been compiled when the module is loaded. This implies that there is no way to get your static initialized during module load except the CLR guys provide you with a hook to call your C#DllMain. But until then there will be no way initialize all statics during module load at once. From a more distant viewpoint it might even be a good thing that there is no such function anyway. If it would be here I hear you already cursing that you want to do something before C#DllMain was called ;) Your seconds problem that the static ctor aka type initializer has not been called when you are inside a static method should not happen except in grave cirumstances when a race condition occured. Suppose two classes A and B where each of them has a static instance of the other type inside it. This was bad design in C++ and is still in C#. If you have a different scenario where this happens I would be glad to hear about it. Yours, Alois Kraus My Blog
-
Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:
cheers, Chris Maunder
CodeProject.com : C++ MVP
Nah... Old age will kill you but .Net will drive you insane.:^):-D
-
Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:
cheers, Chris Maunder
CodeProject.com : C++ MVP
Anyone else mentioned "Do not post programming questions (use the programming forums for that)" ? :rolleyes: