Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Microframework static constructor method

Is this a bug

  • Please log in to reply
5 replies to this topic

#1 asciiman

asciiman

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts
  • LocationTurkey

Posted 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.

 

 



#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 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


#3 Andre Trollip

Andre Trollip

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationGauteng, South Africa

Posted 26 August 2014 - 12:38 PM

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.

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. ;-)



#4 asciiman

asciiman

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts
  • LocationTurkey

Posted 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)



#5 Andre Trollip

Andre Trollip

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationGauteng, South Africa

Posted 27 August 2014 - 12:05 AM

That would be perfectly safe and valid to do.



#6 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 27 August 2014 - 06:19 AM

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)

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 topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.