When is ~<classname> called actually ?
In the below example when is the ~Worker() method called exactly ?
class ch03_11
{
static void Main()
{
Worker worker = new Worker();
worker.Dispose();
}
}
public class Worker: System.IDisposable
{
private bool alreadyDisposed = false;
public Worker()
{
System.Console.WriteLine("In the constructor.");
}
public void Dispose(bool explicitCall)
{
if(!this.alreadyDisposed)
{
if(explicitCall)
{
System.Console.WriteLine("Not in the destructor, " +
"so cleaning up other objects.");
// Not in the destructor, so we can reference other objects.
//OtherObject1.Dispose();
//OtherObject2.Dispose();
}
// Perform standard cleanup here...
System.Console.WriteLine("Cleaning up.");
}
alreadyDisposed = true;
}
public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}
~Worker()
{
System.Console.WriteLine("In the destructor now.");
Dispose(false);
}
}
exactly thats what i have read, but...
i have put a breakpoint on it...
And it is called in neither of the cases
when i explicitly dispose() the object
or when i just skip the disposing.