c# - Does base class gets instantiated for every new instance? -


i have been thinking how clr creates new instances. consider code:

public class base {     public base()     { } }  public class derived : base {     public derived()         : base()     { } } 

these questions:

  1. does system.object instance created every time line of code, var baseobj = new base();? how many instances there in memory? 2 or 1?

  2. for line of code: var derobj = new derived();. how many instances created? 3 or 2?

in both samples, there 1 object. there 1 piece of memory allocated , base class 'merges' parents. there 1 class containing methods, fields , properties derived base classes, including base-class-of-all. object.

what think happen if var derivedobj = new derived(); creates 2 instances? how refer 1 not assigned baseobj? there no use in keeping 2 instances of classes alive. 1 do.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -