What is CrazyEngico.com (CE)?

CrazyEngico (CE) is a one-stop solution for interview questions and answers, domain knowledge, e-learning, tutorials, articles and many more learning material.
e-learning - Intelligent e-learning tutorials and articles
Domain Knowledge Learn about various business domains/verticals such as Banking, Financial, Insurance, Healthcare and many more...
Interview Questions and Answers - Post your questions and answers. 1000+ database of ready questions and answers available to prepare for an interview.
Tutorials - 300+ video tutorials from beginner to expert level.
Articles - 300+ technical articles to understand concepts.
Forum - A technical forum to post your queries/responses.

Search Questions

Interview Questions/Answers

Ads
Question
Interview Q & A >> C# >> Difference between Finalize and Dispose

<< Previous Question Navigator Next >>

0        0
Difference between Finalize and Dispose (Views: 2209)


Answers

2        1

Class instances often encapsulate control over resources that are not managed by the runtime, such as window handles (HWND), database connections, and so on. Therefore, you should provide both an explicit and an implicit way to free those resources. Provide implicit control by implementing the protected Finalize method on an object (destructor syntax in C# and the Managed Extensions for C++). The garbage collector calls this method at some point after there are no longer any valid references to the object.
In some cases, you might want to provide programmers using an object with the ability to explicitly release these external resources before the garbage collector frees the object. If an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used. To provide explicit control, implement the Dispose method provided by the IDisposable Interface. The consumer of the object should call this method when it is done using the object. Dispose can be called even f other references to the object are alive.
Note that even when you provide explicit control by way of Dispose, you should provide implicit cleanup using the Finalize method. Finalize provides a backup to prevent resources from permanently leaking if the programmer fails to call Dispose.

Submitted By: Anonymous | Created On: 5/16/2009 4:42:47 PM


2        0

Finalize:  Finalize method is not called by itselft, when we use finalize methods it's become expensive for developer. It completed in two rounds, first it will mark all unwanted resources and keep it inside the finalization queue and when the next time reference checking garbage collection execute, all the marked object will be distroyed from the memory. It runs automatically we can not call it. It Make sure that unwanted resources will be free when the Garbage collection runs next time. It is always adivisable do not use Finalize method.

Dispose(): Dispose method is also used for the same purupose, however it is helpful when you want to release memory forcefully. It belongs to IDisposable. It is useful when we have custom classes. It is always advisable to use dispose method wehn you want to clean your memory from unmanged resources. It is the implicit way to distroying any object, we can implement dispose method from IDisposable. Here GC will not come in to picture, you can decide when you want to clean up resources. This also not called implicitly you have to call it manually.

www.codinghub.net



Submitted By: James | Created On: 12/12/2011 9:23:14 AM


Post your Answer
Your answer will be first reviewed by our administrator before it appears on the site.

Your Answer:


 
Display Name:





By clicking on Submit button you agree to the Terms of Service agreement.




Copyright © 2018 CrazyEngico.com. All rights reserved.
CrazyEngico.com is not responsible for the content and CrazyEngico.com does not evaluate or guarantee the accuracy of any CrazyEngico.com content.
Please read our terms of service agreement before using this site.

feedback