Search code examples
c#.netgarbage-collectionobject-reference

C# garbage collection with object references


In C#, when I have two object obj1, obj2 composed of a List<string> and I assign both of those objects to the same List<string> object.

If my reference to obj1 goes out of scope but my reference to obj2 does not, is obj1 still eligible for garbage collection, or are there some dependency issues because there is still a reference to the List<string> object?


Solution

  • In your case, obj1 must be eligible for garbage collection.

    You need to look at Jon Skeet's answer here. It clearly explains how garbage collection works on object references.

    A nice tutorial for you on Object's Lifetime in C#.