|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Question about DestructorsHi All,
When should one use the Finalize method and when the Dispose method? kd "kd" <k*@discussions.microsoft.com> schrieb: ..NET Framework Developer's Guide -- Programming for Garbage Collection> When should one use the Finalize method and when the Dispose method? <URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconprogrammingessentialsforgarbagecollection.asp> -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> KD,
> When should one use the Finalize method and when the Dispose method? One of the advantages from managed code is that it does what it says it is for. It manages your code and especially in that the removing of used objects. So the best approach is not doing both as long as you uses the dotnet classes (with some small exceptions however when it is not written than forget those). When you start building your own classes from scratch than you have to watch to use the Idisposable interface when you are using unmanaged resources (dont mix that up with managed code). http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIDisposableClassTopic.asp Because that most classes we use are derived from the component class which implements this, is this mostly implemented. The best what I have read (although probably as well still incomplete is this) http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/msg/3e55c043b1e6ae59 (Impliciet in this text is when it is by instance a class that is created using the form or the component designer, than you see it in that). I hope this helps, Cor Hi kd,
Implement IDisposable and use Finalize() when you're holding onto *any* unmanaged resources that you need to persist across the lifetime of your object. Database connections are a big one - create a connection in the constructor, stuff it into a member and reference it when you need it. But you have to close it sometime! :) You'll have to check out the resources provided by Cor and Herfried - explaining the intricacies in any great detail would take a lot of writing. Suffice it to say that unmanaged code cleanup goes into Dispose(), and a call to Dispose() goes into Finalize(). This ensures that unmanaged resources are released - either manually by the programmer (which is the *best* way), or automatically when the object undergoes garbage collection. Use GC.SuppressFinalize(Me) in Dispose() to prevent your destructor from being executed. This is necessary to ensure that you don't attempt to call Dispose() twice and hence free the same resources twice! Regards, -Adam. kd wrote: Show quoteHide quote > Hi All, > > When should one use the Finalize method and when the Dispose method? > > kd Oh, I should point out this is just my POV. It's really a kind of holy
war on this sort of thing :)
Determine if "Hide extensions for known file types" is active
Need help updating table What scope is best for defining Enum type? VB6 to VB.Net Conversion Go to URL on Button_Click Errors when translating CultureInfo from C# to VB Selecting specific columns from a dataview? How to overload method of third party component? StringBuilder size question? Limiting the directories somebody can browse to..... |
|||||||||||||||||||||||