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.
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
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.
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
This article is related to
C#,.NET,Architect,Intermediate,VS2010,.Net,Articles,Computer Tutorials, Dispose, Finalize, Garbage Collector, Interview Questions
C#,.NET,Architect,Intermediate,VS2010,.Net,Articles,Computer Tutorials, Dispose, Finalize, Garbage Collector, Interview Questions
No comments:
Post a Comment