Is this a bug.
In .Net Microframework, all static constructor methods fired immediately.
In .Net Framework, constructor methods fired when access the class.
  | ||||||||||||||
5 replies to this topic
#1Posted 24 August 2014 - 09:53 PM Is this a bug.
In .Net Microframework, all static constructor methods fired immediately. In .Net Framework, constructor methods fired when access the class.
#2Posted 25 August 2014 - 06:36 AM No, it's a feature of C#.
As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program.
It's up to the run time system to determine when the static constructor is executed.
Regards, Mark To be or not to be = 0xFF
Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life Follow @nevynuk on Twitter #3Posted 26 August 2014 - 12:38 PM
To put it differently, the only guarantee that the CLR makes (with regards to static constructors) is that the static constructor will have run before any member (field, property or method) is accessed. Regardless of whether the accessed member is static or not. Other than that, you should not rely on a static constructor running at a specific point in time (or order).
As long as the CLR doesn't breach this principal, it can be implemented differently for the tinyCLR, .Net CLR, Any CLR etc. ;-) #4Posted 26 August 2014 - 05:50 PM hi,
This is not good because i am reading settings from xml file in static void main { } block and then assign this values to other static members in Information.cs (class) after this I use this values when need. (Access the corresponding class) #5Posted 27 August 2014 - 12:05 AM That would be perfectly safe and valid to do. #6Posted 27 August 2014 - 06:19 AM
There should not be a problem with this as the CLR will ensure that the class is instantiated and the static constructor is called before you access any members or call any methods.
It does not matter if this is .NET on Windows or .NET on Microframework, the same rule applies. The only thing which changes is exactly when things happen. The logical sequence will always be the same (i.e. constructor is always called before method call/member access), the exact time can change but not the logical sequence.
Regards, Mark To be or not to be = 0xFF
Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life Follow @nevynuk on Twitter 0 user(s) are reading this topic0 members, 0 guests, 0 anonymous users | ||||||||||||||
|
||||||||||||||
This webpage is licensed under a Creative Commons Attribution-ShareAlike License. | ||||||||||||||