You must Sign In to post a response.
  • Category: .NET

    Destructor Finalize or Dispose

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.
    Can anyone explain the difference between these 3 things viz. Destructor , Finalize method and Dispose method. I mean where should we use which type of cleanup code.......
  • #2369

    All these things are used to perform object cleanup, but they are all a
    little different.

    These are called by the CLR when the Garbage Collector destroys an object.
    In VB.NET it is called the Finalize() method. In C# it is declared by
    putting a tilda '~' in front of the class name (~MyClass() {}).
    Destructors aren't the best place to put clean up code. This is because it
    is only called by the GC and you can't determine when it will be called (you
    can force it to be called, but that's not recommended). Thus, if you open a
    database and you put the cleanup code in the destructor, that database
    connection could potentially be open for the entire life of the project. Or
    it might only stay open for only a moment, but you can't control this. The
    best rule of thumb is to leave Destructors alone until you get into more
    advanced programming.

    Dispose methods:
    The Dispose() method is simply a method you call manually to clean up your
    object. In reality, this is just another method of your class and you can
    call it whatever you terminate the object. If you are a VB6 programmer, just
    think that whereever you would set 'object=Nothing' now just call the
    Dispose() method. You should put all cleanup code here rather than in the
    destructor. Whenever you open an object that is resource intensive, you
    should call its Dispose() method. As far as the method name is concerned,
    it's a standard to call it Dispose() and you should really stick to that.
    Ideally, you will also have your object implement the IDispose interface.
    This is beneficial in C# if you are accessing a class with the 'using'
    statement b/c it automatically calls the Dispose() method when it finishes.
    As far your question regarding the DisposeObject() method, I personally
    haven't heard of it before. But I have to assume that it is just another
    name for the Dispose() method. If you see someone using DisposeObject() in
    C# or VB.NET I suggest that you don't adopt that habit yourself since
    Dispose() is the recommended naming standard.


  • #2371
    Thanks Kamlesh info is useful . Even i am talking about Dispose() method and not DisposeObject(). Still some more doubts ...
    1) So does this means that destructor and Finalize is something which is same i.e. Finalize in VB.NET is equivalent to destructor in C#.NET. There's no significance of a Finalize method in C#??
    2) I had read somewhere that you should cleanup the unmanaged resources used in the class in Dispose method and give a call to it explicitly. Is that true.
    3) Is Dispose method called by garbage collector in any case ?
    4) What will happen if i've some unmanaged resources in a class and i do not clean them up in any of the cleanup methods?

  • #2389

    In the managed world we are mostly relieved of finalizing objects. MSDN recommends to call dispose only if the object is really resource intensive. If you use unmanaged com objects then you must make sure they are propery terminated since GC dosn't take care of that.

    Calling finalize directly has got a performance cost. Have a look at this code project articles

    Thanks and Regards

  • #285361
    Finalize :
    1.Finalize() is called by the runtime
    2.Is a destructor, called by Garbage Collector when the object goes out of scope.
    3.Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.

    Dispose :
    1.Dispose() is called by the user
    2.Same purpose as finalize, to free unmanaged resources. However, implement this when you are writing a custom class, that will be used by other users.
    3.Overriding Dispose() provides a way for the user code to free the unmanaged objects in your custom class.

    Verify these links for more ideas..

    Sridhar R
    Nothing is illegal, Until You Get Caught

  • #297989

    Following the difference between Dispose and Finalize method,

    1>CLR uses the Dispose and Finalize methods for performing garbage collection of runtime objects of .Net applications.

    2>Clr has a Garbage Collector(GC) which periodically checks for unused and unreferenced objects in Heap.It call Finalize() method to free the memory used by such objects.

    3>Dispose is another method which is invoked by Garbage Collector to release the memory occupied by an object.Dispose method needs to be explicitly called in code for removing an object from Heap.

    4>Dispose method can be invoked only by the classes that IDisposable interface.


    Thanks & Regards


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.