views:

890

answers:

7

I have a C# object with a critical resource that needs to be flushed very specific points in time making it a bad candidate to leave around for the garbage collector to take care of whenever it gets around to it, how should I handle this? Is there something like C++'s delete operator that will let me manually kill the instance of this object when needed?

+3  A: 

Since .NET has garbage collection, there is no deterministic finalization. Microsoft provides the IDisposable interface, but it's cumbersome to use (check out this article for more on that).

Ben Hoffstein
+2  A: 

This is precisely what the IDiposable interface is for. You release the critical resources in the Dispose() method, and then leave the object around for the garbage disposer to deal with deallocating the memory.

David
A: 

Google for the IDisposable interface. This is the only mechanism available to you. It's tailor made if your critical resource is unmanaged. If it's a managed resource, could you be more specific about what needs to be "flushed".

Joe
A: 

The IDisposable interface was added to support deterministic destruction in C++/CLI, and you can use it from any .NET language. It's what you want.

Nick
A: 

If you are talking about a specific managed resource that you feel "MUST" be released at a specific time, you could specifcally call the Garbage Collectors' Collect method, after de-referencing the object, but there are performance considerations to be thought of, as normally the garbage collector knows when to collect items. And in general it is a bad idea.

As others are mentioning above, the IDisposable pattern is helpful for releasing unmanaged resources when needed.

NOTE: I'm going to repeat, you COULD call GC.Collect() but it is NOT a good thing, but a valid answer for the question!

Mitchel Sellers
Don't call GC.Collect(). Bad habit to get into, and is usually more detrimental than useful.
Will
I know that...thus my note about not using it.
Mitchel Sellers
+1  A: 

The IDisposable interface exists for deterministic destruction. There's a pattern for implementing it correctly on MSDN.

In tandem, you should also consider using the using statement when your object's lifetime does not span multiple scopes.

Greg D
+8  A: 

You are looking for IDisposable. Here is an example class that implements this.

class MyDisposableObject : IDisposable
{
   public MyDisposableObject()
   {
   }

   ~MyDisposableObject()
   {
      Dispose(false);
   }

   private bool disposed;
   private void Dispose(bool disposing)
   {
      if (!this.disposed)
      {
          if (disposing)
          {
             // Dispose of your managed resources here.
          }

          // Dispose of your unmanaged resources here.

          this.disposed = true;
       }
   }

   void IDisposable.Dispose()
   {
      Dispose(true);
      GC.SuppressFinalize(this);
   }
}

To use it, you can do something like this:

public void DoingMyThing()
{
   using (MyDisposableObject obj = new MyDisposableObject())
   {
      // Use obj here.
   }
}

The using keyword makes sure that the Dispose() method on IDisposable gets called at the end of its scope.

Jeff Yates