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?
Printable View
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?
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.
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.
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?
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
Suppose the code below, which is from a very simple console demonstration app :
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?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();
}
}
}