0

How to Dispose managed or unmanaged objects Using Dispose Method?

In my application class is implemented IDisposable interface and gave overridden method Dispose(). But my actual doubt is how to dispose managed or unmanaged code with in the Dispose method.

public override void Dispose()
{
   // What should i do for my unmanaged objects?

   // can i make my object set to 'null'?
}

1 Answers1

0

The dispose method works together with the using statement. It will automatically called if a using block is closed.

class A : IDisposable
{
    public void Dispose()
    {
        // Dispose
    }
}

using (A a = new A())
{

}

You also don't need to override the method, because it is defined in an interface.

In any case using is leaved, dispose will be called.

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • I agree BendEg. My actual question is how to dispose objects with in Dispose method? should i set object=null? – Vijay Kumar Bachu Sep 30 '15 at 07:01
  • No, why should you do this? That's not what the dipose method is created for. Maybe this is a nice article for you: http://ericlippert.com/2015/05/18/when-everything-you-know-is-wrong-part-one/ – BendEg Sep 30 '15 at 07:15