[Mono-devel-list] possible bug in static constructor evaluation order

Michal Moskal malekith at pld-linux.org
Sun May 4 17:29:31 EDT 2003

On Sun, May 04, 2003 at 03:20:09PM -0500, 2a5gjx302 at sneakemail.com wrote:
> My C# specification (the 'C# Language Specification.doc' file 
> distributed with VS.NET) does not have a section 17.11. The entire 
> section 17 is about attributes. 

Hmm, I should have given a link:

> I should point out that, as far as I understand it, this is no bug. The 
> specification does not state precisely when static construction occurs, 
> only that it must occur prior to any use of the class. 
> My specification states these things: 
> - A static constructor for a type is run at most once per application 
> domain. 
> - Static variables come into existence at some undetermined time prior 
> to the execution of the static constructor. 
> - The static field variable initializers of a class correspond to a 
> sequence of assignments that are executed at an 
> implementation-dependent time prior to the static constructor of the 
> class (if any) and prior to the first use of a static field of that 
> class.

Ok, modulo ^^^^:

  If a class contains any static fields with initializers, those
  initializers are executed in textual order immediately prior to
  executing the static constructor.

> ..and most importantly: 
> The exact timing of static constructor execution is 
> implementation-dependent, but is subject to the following rules: 
> - The static constructor for a class executes before any instance of 
> the class is created. 
> - The static constructor for a class executes before any of the static 
> members for the class are referenced. 
> - The static constructor for a class executes after the static field 
> initializers (if any) for the class. 
> - The static constructor for a class executes at most one time during a 
> single program instantiation.

Consider Example in http://www.jaggersoft.com/csharp_standard/17.11.htm.
It *must* produce output:

Init A  
Init B  

But according to what you wrote, it could be also:

Init A
Init B


Init B
Init A

This is where I took impression that static constructors are executed
just before first access to class.

Also Paragraph 6 states:

(1) The static constructor for a class executes at most once in a given
application domain. (2) The execution of a static constructor is triggered
by the first of the following events to occur within an application

    * (3) An instance of the class is created.
    * (4) Any of the static members of the class are referenced. 

I would interpret 'A is triggered by B' as: A occurs just before B.

Ah, and what do I need it for. I want to formally define some aspects of
class loading using operational semantics, so I need precise rules.

: Michal Moskal :: http://www.kernel.pl/~malekith : GCS {C,UL}++++$ a? !tv
: PLD Linux ::::::::: Wroclaw University, CS Dept : {E-,w}-- {b++,e}>+++ h

More information about the Mono-devel-list mailing list