Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This boils down to coding preference as stated in one of the comments. If you compile the following code</p> <pre><code>public class TestInitialization { private object test1 = new object(); private object test2; public TestInitialization() { this.test2 = new object(); } } </code></pre> <p>When compiled, the actual code used is as follows</p> <pre><code>public class TestInitialization { private object test1; private object test2; public TestInitialization() { this.test1 = new object(); this.test2 = new object(); } } </code></pre> <p>So they are exactly the same thing, use whichever you prefer.</p> <p>EDIT: Here is an example of a base class with an inherited class and the resultant compiled IL.</p> <p>Base class</p> <pre><code>class basetest { private object test1 = new object(); private object test2; public basetest() { this.test2 = new object(); } } </code></pre> <p>Inherited class</p> <pre><code>class testclass : basetest { private object testclass1 = new object(); private object testclass2; public testclass() : base() { this.testclass2 = new object(); } } </code></pre> <p>Resulting IL Base class</p> <pre><code>.class private auto ansi beforefieldinit basetest extends [mscorlib]System.Object { .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { .maxstack 8 L_0000: ldarg.0 L_0001: newobj instance void [mscorlib]System.Object::.ctor() L_0006: stfld object logtest.basetest::test1 L_000b: ldarg.0 L_000c: call instance void [mscorlib]System.Object::.ctor() L_0011: nop L_0012: nop L_0013: ldarg.0 L_0014: newobj instance void [mscorlib]System.Object::.ctor() L_0019: stfld object logtest.basetest::test2 L_001e: nop L_001f: ret } .field private object test1 .field private object test2 } </code></pre> <p>Inherited class IL</p> <pre><code>.class private auto ansi beforefieldinit testclass extends logtest.basetest { .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { .maxstack 8 L_0000: ldarg.0 L_0001: newobj instance void [mscorlib]System.Object::.ctor() L_0006: stfld object logtest.testclass::testclass1 L_000b: ldarg.0 L_000c: call instance void logtest.basetest::.ctor() L_0011: nop L_0012: nop L_0013: ldarg.0 L_0014: newobj instance void [mscorlib]System.Object::.ctor() L_0019: stfld object logtest.testclass::testclass2 L_001e: nop L_001f: ret } .field private object testclass1 .field private object testclass2 } </code></pre> <p>I think there's a bit of confusing on my part. In this example the initializer outside of the constructor is initialized FIRST just before the base constructor is called. So no matter what, initializers outside of a constructor will be initialized first before the ones inside a constructor and for the most part this shouldn't matter. Technically they all get converted to be intialized inside a constructor and the following rules apply.</p> <ol> <li>All initializers outside of a constructor are run first</li> <li>All base class constructors are called</li> <li>All initializers inside constructor are run</li> </ol> <p>Basically the compiler prepends all initializers outside the constructor to the constructor code and then runs as normal.</p> <p>So this</p> <pre><code>public class test : basetest { private object test1 = new object(); private object test2; public test() : base() { this.test2 = new object(); } } public class basetest { private object basetest1 = new object(); private object basetest2; public basetest() { this.basetest2 = new object(); } } </code></pre> <p>becomes</p> <pre><code>public class test : basetest { private object test1; private object test2; public test() { //prepend everything first this.test1 = new object(); //call base base(); //not legal but just an example //everything else that was already here this.test2 = new object(); } } public class basetest { private object basetest1; private object basetest2; public basetest() { //prepend initializers this.basetest1 = new object(); //if there were more base classes, the constructors would be called here //do everything else that was already here this.basetest2 = new object(); } } </code></pre> <p>Hopefully that makes more sense and clears some things up. I know I was having an issue understand what some people were meaning when they said it runs "first" or "outside" of the constructor; it does in fact ALL run INSIDE a constructor but the order of what is called is affected.</p>
 

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