WHAT IS THE DIFFERENCE BETWEEN FINALIZE() AND DISPOSE()?

John William

Total Post:5

Points:35
Posted by  John William
C# 
C#
 4789  View(s)
Ratings:
Rate this:
Please Explain
  1. Amit Singh

    Post:103

    Points:721
    Re: What is the difference between Finalize() and Dispose()?

    Difference between Finalize() and Dispose() method
    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.

    Dispose() method permanently removes connection object from memory and the resource no longer exists for any further          processing.


      Modified On Mar-16-2011 01:00:11 AM
  1. Awadhendra Tiwari

    Post:126

    Points:882
    Re: What is the difference between Finalize() and Dispose()?

    Design Pattern : If your classes use unmanaged resources, you need to implement both Dispose & Finalize. Dispose() is
    called by user code, that is, the code that is using your class.

    Finalize/Destructor cannot be called by User code, it's called by Garbage
    Collector

    Finalize : Is a destructor, called by Garbage Collector when the object goes
    out of scope. 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 : 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. Overriding Dispose() provides a way for the user code to free the
    unmanaged objects in your custom class
                   

  1. Lakshmi Narayanan

    Post:5

    Points:35
    Re: What is the difference between Finalize() and Dispose()?

    Hi,
     refer this link
    http://www.geekinterview.com/question_details/37506

  1. yang welar

    Post:1

    Points:7
    Re: What is the difference between Finalize() and Dispose()?

    Finalize()

    The Garbage Collector calls this method at some point after there are no longer valid references to that object in memory.

    The framework does not guarantee that when this will happen, you can force for Garbage Collection but it will hurt performance of your program. Finalize() belongs to the Object class and it will be called by the runtime.

    Dispose()

    There are some resources like windows handles, database connections , network connections, files, etc. which cannot be collected by the Garbage Collector. If you want to explicitly release some specific objects then this is the best to implement IDisposable and override the Dispose() method of IDisposable interface.

    source:

    http://net-informations.com/faq/framework/finalize-dispose.htm

     

    yang.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!