Tech Off Post

Single Post Permalink

View Thread: is 'new object().property' or 'new object().method()' a good idea?
  • User profile image
    Curt Nichols

    littleguru wrote:
    The MyObject instance won't be collected by the GC if you are accessing any fields that held by that instance... otherwise - as said - it would be a heavy CLR (GC) bug!

    This is incorrect, and it's not a bug. Objects referenced by fields of the MyObject instance will be collected only if when are no rooted references to them, not when the MyObject instance is collected. The MyObject instance may be collected even if there are other outstanding references to objects that its fields refer to. You can see for yourself here, code is included.