Yes, you're right. And the nice thing is you can prove it by using a WeakReference
.
A WeakReference
is an object which tracks another reference, but without preventing it from being collected. This allows you to check your target reference any time, and see if it has been collected yet:
private static void Main(string[] args)
{
MyInt x = new MyInt();
x.MyValue = 3;
MyInt y = new MyInt();
WeakReference reference = new WeakReference(y);
Console.WriteLine("Y is alive: " + reference.IsAlive);
y = x;
y.MyValue = 4;
Console.WriteLine("Y is still alive: " + reference.IsAlive);
Console.WriteLine("Running GC... ");
GC.Collect(2);
GC.WaitForFullGCComplete();
Console.WriteLine("Y is alive: " + reference.IsAlive);
Console.Read();
}
This code proves your point, output below:
Y is alive: True
Y is still alive: True
Running GC...
Y is alive: False