Should I use Dispose in this situation

hello,

i'm initiating an object inside a method:

private void Example ()
{
MyClass ins = new MyClass(...);
ins.AMethod();
ins.Dispose();
}

i'm wondering if the last line: ins.Dispose(); is necessary Or C# does it automaticlly ... i mean disposing the object as soon as the method is finish.

please advise


Answer this question

Should I use Dispose in this situation

  • JCJCJC

    A lot of good advice. It is kinda straight-forward. If this is your own class, what would you dispose of when you implement IDispose If it is not your own class, or your class has a member that might implement IDispose, look through the docs and see if that class implements IDispose. If it does, you should implement IDispose, it it doesn't, you shouldn't.

    If you should call Dispose, implement your code with the VS2005 "using" keyword.

    Don't worry too much about it, if you get it wrong it won't hurt much. The compiler will complain if you try to Dispose an object that won't let you. Your program will be a bit more resource-hungry if you forget to Dispose an object that will let you. From what I've seen, the garbage collector not only runs when you've used a lot of memory but also runs when you've used up a lot of Windows handles...

    One more detail: if an object is handed to you by some external code, don't call Dispose on it. The e.Graphics object in the Paint event of Windows Forms controls is a good example.



  • NickNotYet

    Hi,

    As a good practice always dispose your objects, no matter if they live in a method. In your example, as your object loses the reference as soon as the method ends, it is ready for garbage collection. This means that sooner or later your object will be disposed. The problem is that you don't know when. What is more, if your object holds an unmanaged resource this will remain open, and i the case that your class does not implement a proper finalizer (not dispose) a memory leak will appear.

    If you design your own class that consumes an unmanaged resource I recommend:

    public class YourClass : IDisposable
    {
    public YourClass(){ }

    ~YourClass()
    {
    Dispose();
    }

    public void Dispose()
    {
    // Dispose your unamanged resource
    GC.SuppressFinalize(this);
    }}

    By this way, if the class user does not call your dispose method, the Garbage collector will properly dispose all your unmanaged resources.

    Hope this helps.

    Best regards



  • CoDeR X

    Hi,

    It is very advisable to call an objects Dispose method. If the object is implementing IDisposable, or has a dispose method available then you'll need to call it. In your example, after the method finishes executing your object would be removed by the garbage collector. But what if that object uses a umananged resources That is when memory leaks appear. So as a precaution, always call dispose methods on objects.

    cheers,

    Paul June A. Domag



  • ITJoeB

    It depends on the definition of MyClass. Dispose isn't something you do on every object, and not every class needs to implement IDisposable.

  • mahima

    If you don't use any unmanaged/native resources like handles and etc you needn't realise Dispose.

    C# can't clear resources automatically, for this you need realise Dispose/Finalize



  • Should I use Dispose in this situation