|
-
Oct 19th, 2005, 02:53 AM
#1
Thread Starter
Hyperactive Member
When to use a destructor
Hello everybody
I`m bit confused as to when I need to implement a destructor for an object in C#. I`ve read that it is only required for unmanaged code. But what exactly does that mean?
-
Oct 19th, 2005, 05:12 AM
#2
Re: When to use a destructor
Unmanaged code is anything that doesn't use the .NET Framework, so basically COM and APIs. If your classes use unmanaged resources that need to be cleaned up, or they use other .NET classes that do, then you should implement a Dispose method. Managed classes that use unmanaged resources will basically be those that implement a Dispose method themselves, so your Dispose method may just call Dispose on a member variable or two.
-
Oct 19th, 2005, 09:04 AM
#3
Lively Member
Re: When to use a destructor
Even if you're using managed code, Using a destructor can still be useful for if you want things to happen upon the disposal of a class. For example, if you want to decriment (or incriment) a value when your class moves on to a better place.
-
Oct 20th, 2005, 02:54 AM
#4
Thread Starter
Hyperactive Member
Re: When to use a destructor
The problem with the destructor in C#, is that it is only called when the garbage collector decides so. So you can`t base your code on the fact that the destructor will be called when your object goes out of scope.
But what about a Dispose method? In what way is it different from a Destructor? Do I merely need to add a method named Dispose to my class? And when will it be called?
-
Oct 20th, 2005, 03:07 AM
#5
Re: When to use a destructor
The idea is that you implement a Dispose method and a Finalize method. The Dispose method should be called in code when the object is no longer needed. The Finalize method is a fail-safe to ensure that the Dispose method is called when the GC is ready to clean up the object whether the developer remembered to or not.
http://msdn.microsoft.com/library/de...izeDispose.asp
-
Oct 20th, 2005, 03:58 AM
#6
Thread Starter
Hyperactive Member
Re: When to use a destructor
Suppose the code below, which is from a very simple console demonstration app :
Code:
using System;
using System.Text;
namespace Testing
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
public class Testing : IDisposable
{
// private member property
private bool is_disposed = false;
// overloaded virtual protected Dispose member method
protected virtual void Dispose(bool disposing)
{
if (!is_disposed)
{
if (disposing)
{
Console.WriteLine("Not in destructor, OK to reference other objects");
}
// Perform cleanup for this object.
Console.WriteLine("Disposing...");
}
this.is_disposed = true;
}
// virtual protected Dispose member method
public void Dispose()
{
Dispose(true);
// tell the GC not to finalize.
GC.SuppressFinalize(this);
}
~Testing()
{
Dispose(false);
Console.WriteLine("In destructor.");
}
}
class Tester
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Testing tstObject = new Testing();
tstObject.Dispose();
}
}
}
In this code I can see that the Dispose method is being called. But what about teh Destructor? The Message in the destructor is not being written on screen before the app executes. Is there something wrong with the approach?
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
|