Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe the problem is that you're relying on the static initializer for <code>Elements</code> having run, despite the fact that you haven't referred to any fields within it. The type initializer in a type which doesn't have a static constructor is only guaranteed to run before the first static field access. Section 10.5.5.1 of the C# 5 specification:</p> <blockquote> <p>If a static constructor (§10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.</p> </blockquote> <p>And section 10.12 has:</p> <blockquote> <p>The static constructor for a closed class type executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:</p> <ul> <li>An instance of the class type is created.</li> <li>Any of the static members of the class type are referenced.</li> </ul> </blockquote> <p>The implementation of type initialization <a href="http://msmvps.com/blogs/jon_skeet/archive/2010/01/26/type-initialization-changes-in-net-4-0.aspx" rel="noreferrer">changed in .NET 4</a>, but it was only an implementation detail - your code was broken before, you just didn't know it.</p> <p>If you change your code to have:</p> <pre><code>static Elements() {} </code></pre> <p>in the <code>Elements</code> class, <em>then</em> I believe it will work - because static constructors force type initialization to occur before immediately the first <em>member</em> access, rather than just "at some point before the first <em>field</em> access".</p> <p>Personally I'm dubious about the general pattern, but that's a slightly different matter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload