Thursday, July 18, 2013

Object Creation


Constructors: Constructors are special method that allow an instance of a Type to be initialized in good state.Unlike other methods they are never inherited. If you have written some code in constructor then complier will first emit code which initializes instance field than write code u have written in the compiler, it means if you are using inline initialization of some instance field than first it initialize to inline value and then it will be overwritten by value u passed in constructor.
Reference Type Constructors:
  • For verifiable code instance constructor must call it's base class's constructor before accessing any of the inherited field of the base class.Compiler will always emit a call to base class parameterless constructor.
  • If no constructor is explicitly define C# compiler will define a default parameterless constructor for you, which is public by default, protected for abstract classes. For static classes it wouldn't emit any default constructor because there is no instance field in static classes even if there is static field u can't update it's value in constructor because parameterized constructor is not allowed in staic classes so only possiblity is inline initialization or initializing it to some const value in parameter less static constructor.
  • If you are defining a parameter less constructor explicitly you have to make it public becuase by default methods are private in C#.Call to base class constructor will be emitted by c# compiler anyway.
  • If you are defining parameterized constructor then there will be no parameter less constructor emited by compiler.remember compiler will always emit call for base class parameterless constr. So if you have define parametrized constr in derived and base class and haven't called the base constr. from derived constr. you will get an compilation error as there wouldnt be any paramless constr. in base class and compiler has emitec call to call base paramles constr, now if you delete a parameterized cnostr. in base class this force compiler to emit code for parmles constr in base class and code will compile or you can explicitly put call to base paramterized constr from derived const.
  • Object can also be created without calling constructor by either Using MemberwiseClone method or by de-serializing/serializing an object.
  • All constructor parameterized or parameter less call system.object's constructor.
  • If initializing instance field in class def than all constructors will emit the code to initialize it so better to initialize in default constructor and call it in other constructor using this() keyword
TYPE CONSTR. OR STATIC CONTR. OR TYPE INITIALIZER
  • Tpye constructor is required to initialized static instance field. They can be define for both reference type as well as for value types.
  • Only one - that too private and parameter less - can't define access modifier on type constructor. As your code never call type constructor it is CLR which will be calling type constr. and is capable of calling private constr. as well.
  • Executes only once per app domain. Thread safe
  • If there is error in type initializer than CLR consider the type to be unusable.
  • Shouldn’t rely on order of the execution for type constructors.
  • Types are unloaded only when appdomain are unloaded.
  • CLR doesn't support static finalize method so to know appdomain shutdown registers callback with shutdown event.
  • Static type and fields are not inherited so base type's type constructor shouldn't get called.
  • Static constr. get executed only once but when it depends on the way u define in ur code. If u r defining a static constr. and initializing a instance field in it - It will use precise semantics and is slow where as if you are initializing the instance field inline than it will use before-field-init semantics and is fast. These cases will be marked by a flag in metadata called "beforefieldinit" which c# compiler put while compiling.
  • Precise Semantics - call just before the code that would create the first instance of the type or before code that accesses noninherited field or member of the class.??
  • Before-field-Init - call sometime before code that accesses a noninherited static field.??
Value Type Constructors - Structures:
  • Strut extends from system.valuetype.
  • Compiler wouldn't emit default constructor for value type even it doesn't allow developers to define paramless(default) constr just to make it clear that compiler would never initialze the value type, your code have to explicitly do this. So if you try to have inline initialization for instance field (at struct level) in value type ur code wouldn't compile as it means that there should be a default constr. which should initialize the instance field to inline values and there can't be any default constr. in value type. [Can't do inline initialization]
  • We can define parameterized constr. and that too need to be explicitly called.
  • In parameterized constr. we have to initialized all the instance field of the strct else code will not compile.
  • Any Constructor should initialize all the instance field explicitly.
  • Constructor on value type executed only when called explicitly.
  • Constr. can be paramless iff it is static. Static instance field can be initialized inline at struct level.