My questions are related to my previous post at
http://p2p.wrox.com/topic.asp?TOPIC_ID=50274
class Parent : IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(Boolean disposing)
{
if (disposing)
{
Console.WriteLine("parent managed");
}
Console.WriteLine("parent unmanaged");
}
~Parent()
{
Dispose(false);
Console.WriteLine("parent dtor");
}
}
class Child : Parent
{
protected override void Dispose(bool disposing)
{
if (disposing)
{
Console.WriteLine("child managed");
}
Console.WriteLine("child unmanaged");
base.Dispose(disposing);
}
~Child()
{
Dispose(false);
Console.WriteLine("child dtor");
}
}
class Program
{
static void Main(string[] args)
{
Child c = new Child();
c.Dispose();
Console.WriteLine("end main");
}
}
In my understanding, when I call c.Dispose(), GC.SuppressFinalize(this) will suppress Child.Finalizer only. However, the output differs from what I hope -- both Parent.Finalizer and Child.Finalizer are suppressed. Please help me to digest it.
Note: Since Parent has implemented its Finalizer, Child.Finalizer must not be implemented to avoid twice disposing when we forget to call Child.Dispose(). I create Child.Finalizer here just to support this problem.