|
-
Apr 27th, 2004, 11:10 PM
#1
Thread Starter
Lively Member
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);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|