Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I compiled your code and load it up in ILDASM and got this</p> <pre><code>.method public hidebysig specialname rtspecialname instance void .ctor() cil managed { // Code size 15 (0xf) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldc.i4.7 IL_0002: stfld int32 dummyCSharp.MyClass::myInt IL_0007: ldarg.0 IL_0008: call instance void [mscorlib]System.Object::.ctor() IL_000d: nop IL_000e: ret } // end of method MyClass::.ctor </code></pre> <p>Note the <code>ldc.i4.7</code> and <code>stfld int32 dummyCSharp.MyClass::myInt</code> seems to be instructions to set the default values for the myInt field.</p> <p><strong>So such assignment is actually compiled as an additional assignment statement in a constructor.</strong></p> <p>To detect such assignment, then you will need reflection to reflect on the IL of MyClass's constructor method and look for <code>stfld</code> (set fields?) commands.</p> <hr> <p>EDIT: If I add some assignment into the constructor explicitly:</p> <pre><code>class MyClass { public int myInt = 7; public int myOtherInt; public MyClass() { myOtherInt = 8; } } </code></pre> <p>When I load it up in ILDASM, I got this: </p> <pre><code>.method public hidebysig specialname rtspecialname instance void .ctor() cil managed { // Code size 24 (0x18) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldc.i4.7 IL_0002: stfld int32 dummyCSharp.MyClass::myInt IL_0007: ldarg.0 IL_0008: call instance void [mscorlib]System.Object::.ctor() IL_000d: nop IL_000e: nop IL_000f: ldarg.0 IL_0010: ldc.i4.8 IL_0011: stfld int32 dummyCSharp.MyClass::myOtherInt IL_0016: nop IL_0017: ret } // end of method MyClass::.ctor </code></pre> <p>Note that the extra assigment on myOtherInt that I added was addded <em>after</em> a call the Object class's constructor.</p> <pre><code>IL_0008: call instance void [mscorlib]System.Object::.ctor() </code></pre> <p>So there you have it,</p> <p><strong>Any assignment done <em>before</em> the call to Object class's constructor in IL is a default value assignment.</strong></p> <p>Anything following it is a statement inside the class's actual constructor code.</p> <p>More extensive test should be done though.</p> <p>p.s. that was fun :-)</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