Sunday, December 1, 2013

How object is created using Type/Class? [ClassName] [ObjectName] = new [ClassName()];

From the Book CLR via C#



  1. Calculates the number of bytes required by all instance fields defined in the type and all of its base types up to and including System.Object (which defines no instance fields of its own). Every object on the heap requires some additional members - called the type object pointer and the sync block index - used by the CLR to manage the object. The bytes for these additional members are added to the size of the object.

  2. Allocates memory for the object by allocating the number of bytes required for the specified type from the managed heap; all of these are bytes are then set to Zero (0).

  3. Initializes the object's type object pointer and sync block index members.

  4. The type's instance constructor is called, passing it any arguments specified in the call to new. Most compilers automatically emit code in a constructor to call the base class's constructor. Eventually, System.Object's constructor is called.


After new operator has performed all these operations, it returns a reference to the newly created object.


Hope this answers all of your questions.


No comments:

Post a Comment