It is not a duplicate, I would like to know about this in context of C#.
I have such classes:
public class A
{
public List<string> AList { get; set; }
}
public class B
{
public List<string> BList { get; set; }
}
Imagine, that in row 6
happens Garbage Collection:
1 row: A objectA = new A();
2 row: B objectB = new B();
3 row: objectA.AList = new List<string>() { "1", "2", "3" };
4 row: objectB.BList = objectA.AList;
5 row: objectA = null;
6 row: //Garbage collection occurs
I have two questions:
Will be
objectA
garbage collected? If yes, why? Cause, in my view these objects will be created in a heap like in the following image:My second question is whether the following image is correct about how objects will be allocated in a heap. I mean, is
objectA.AList
placed inside a circle(the above image) or near the circle(the following image):