0

i had a doubt, suppose in my class (say containing class) i have a field instance which is an object of reference type (say another class as example, call it inner class), at run time when the object of containing class is created on the heap, does containing class stores the entire inner class object in it, or containing class stores the reference of the inner class in it ?

    internal class ContaingClass
    {
       private InnerClass objInner; 
    }

would object of ContainingClass have reference of objInner or shall store entire objInner with all it's data in it?

trincot
  • 317,000
  • 35
  • 244
  • 286
mohits00691
  • 137
  • 1
  • 11

2 Answers2

1

Upon construction it would contain a null reference in the field (assuming your constructor doesn't set it to anything). The field could later be assigned to a reference to an instance, but the 'contents' of that instance would not be stored inside the ContainingClass 'contents'.

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
1

When InnerClass is a reference type it's stored as a refence to the instance.

Here is an answer from Eric Lippert that explains the details on what happens during execution.

Community
  • 1
  • 1
PHeiberg
  • 29,411
  • 6
  • 59
  • 81
  • Thanks for the answer, @PHeiberg : one more quesry, if inner instance variable would be of value type, then ContainingClass's object would be having it's value stored in it or reference to the boxed value type object ? – mohits00691 Oct 02 '12 at 15:24
  • The ContainingClass would hold the value of a value type, not a reference. – PHeiberg Oct 02 '12 at 15:30