Field initialization occurs before the constructor is called through the newobj command. It is easy to verify after decompiling the executable with the following C # code:
using System; namespace ConsoleApplication1 { public class A { public int j; } class Program { static void Main(string[] args) { A g = new A(); Console.WriteLine(gj); } } }
Part of the decompiled MSIL code (main method):
As we can see, MSIL uses the newobj to create an instance of class A. According to the following microsoft acticle :
The newobj command selects a new instance of the associated class with ctor and initializes all fields in the new instance to 0 (of the correct type) or null references, as appropriate. He then calls the ctor constructor with the given arguments along with the newly created example. After calling the constructor, an object reference (type O) is now initialized and pushed onto the stack.
If this is not correct, comment, otherwise designate it as the correct answer.
dyatchenko
source share