I am running the code below and the result is totally different when it runs in Release mode. While in Debug mode, it never collects the object of class A and in Reaelse mode it immediately collects the object of class A.
Can someone explain why.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2 {
class Program
{
static void Main(string[] args)
{
A obj = new A();
B bobj = obj.objB;
GC.Collect();
GC.WaitForPendingFinalizers();
while (bobj.isLive)
{
Console.WriteLine("Is Alive!!");
}
Console.WriteLine("Is Dead!!");
Console.ReadLine();
}
}
class A:IDisposable
{
public B objB = new B();
public A()
{ }
~A()
{
objB.Dispose();
}
#region IDisposable Members
public void Dispose()
{
GC.SuppressFinalize(this);
}
#endregion
}
class B:IDisposable
{
public bool isLive = true;
#region IDisposable Members
public void Dispose()
{
this.isLive = false;
GC.SuppressFinalize(this);
}
#endregion
} }