i




 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C#, Vb.net What is the difference between Finalize() and Dispose()
01-05-2011, 05:09 AM
Post: #1
Not Solved What is the difference between Finalize() and Dispose()
What is the difference between Finalize() and Dispose()?
please explain with example if possible.



Best Regards
Priya
Quote
01-05-2011, 05:42 AM
Post: #2
Not Solved RE: What is the difference between Finalize() and Dispose()
Finalize () is called by Garbage Collector implicitly to free unmanaged resources. The garbage collector calls this method at some point after there are no longer valid references to the object. There are some resources like windows handles, database connections which cannot be collected by the garbage collector. Therefore the programmer needs to call Dispose() method of IDisposable interface.
Dispose () of IDisposable interface is called by the programmer to explicitly release resources when they are no longer being used. Dispose () can be called even if other references to the object are alive.

For more detail with example, refer below link
Implementing Finalize and Dispose to Clean Up Unmanaged Resources

[Image: 2604595733.png]
Quote
01-05-2011, 09:37 AM
Post: #3
Not Solved RE: What is the difference between Finalize() and Dispose()
Implicit way of reclamation of object memory, when there are no longer any valid references to the object, is done by garbage collector calling Finalize method or destructor in C#.

In some cases we might want to release expensive resources like windows handles, database connection, file system etc explicitly once we are finished using those object. Instead of depending on garbage collector for freeing up those expensive resources we can explicitly releases those resources by implementing Dispose method of IDisposable interface.

In C#, you can implement a Dispose method to explicitly deallocate resources.

In a Dispose method, you clean up after an object yourself, without C#'s help. Dispose can be called both explicitly, and/or by the code in your destructor. Note that if you call the Dispose method yourself, you should suppress garbage collection on the object once it's gone, as we'll do here.

Thanks Regards
Shafiur Rahman
http://www.createsoftweb.com
Quote
01-05-2011, 05:08 PM
Post: #4
Not Solved RE: What is the difference between Finalize() and Dispose()
hi priya,

Some quick points on Finalize() and Dispose() in C#:

1. Finalize() is the C# equivalent of destructor ~Object() syntax in C#. In VB.Net you implement the Finalize() by overriding it. But, in C# the compiler translates the destructor to a Finalize() method.

2. Finalize() can NOT be overridden or called in C#.

3. Since, Finalize() is called by the Garbage Collector, it is non-deterministic.

4. Dispose() has to be implemented in classes implementing IDispose interface.

5. Its the right place for freeing-up unmanaged resources like file, handles, and connections etc.

6. Dispose() method is called explicitely in the code itself.

7. Dispose() method is automatically called (for objects which implement IDispose), when used in a "using" statement.

Difference between Finalize and Dispose Method in .net :

C# Provides two special methods that are used to release the instance of a class from memory, Finalize() and Dispose().

Finalize(): The Finalize destructor is a special method that is called from the class to which it belongs or from the derived class. The Finalize() destructor is called after the last reference to an object is released from the memory. The .Net Framework automatically runs the Finalize() destructor to destroy objects in the memory. However, it is important to remember that the Finalize() destructor may not execute immediately when an object loses scope. It is called by CLR using reference-tracing garbage collection, the CLR periodically check for objects that are not used by the application. When such an object is found, the Finalize() destructor is called automatically and the garbage collector of the CLR release the object from the memory.

Dispose(): The Dispose() method is called to release a resource, such as database connection, as soon as the object using such a resource is no longer in use. Unlike the Finalize() destructor, the Dispose() method is not called automatically and you must explicitly call it from a client application when an object is no longer needed. The IDisposable interface contains the Dispose() method. Therefore , to call the Dispose() method, the class must implement the IDisposable interface.

Please give reputation if my answer/post is useful for you
Best Regards.

Thanks & Regards,
Quote
05-21-2013, 07:41 AM
Post: #5
Not Solved RE: What is the difference between Finalize() and Dispose()
For getting a better answer I suggest you please visit this URL : http://planetofcoders.com/difference-finalize-dispose/
Quote
05-27-2013, 11:00 AM
Post: #6
Not Solved RE: What is the difference between Finalize() and Dispose()
(01-05-2011 05:09 AM)Priya Wrote:  What is the difference between Finalize() and Dispose()?
please explain with example if possible.

Finalize()

Finalize() is called by the Garbage Collector before an object that is eligible for collection is reclaimed. Garbage collector will take the responsibility to deallocate the memory for the unreferenced object.

Dispose()

The Dispose() method is not called automatically and you must explicitly call it from a client application when an object is no longer needed. Dispose() can be called even if other references to the object are alive.

more on this topic ...

http://net-informations.com/faq/framewor...ispose.htm

welar
Quote




User(s) browsing this thread: 2 Guest(s)