I'm no expert, but I understand there is a "proper" way to implement IDisposable to ensure that things always get cleaned up properly.
Implementing IDisposable allows you to cleanly release your resources as soon as practical, but relies on the consumer to call the Dispose method properly. So, to get around this, you can also define a Finalizer for your class that performs your cleanup, in the event that IDisposable is not called. The problem is that Finalizers have a bunch of caveats attached to them (related to performance and memory consumption) and thus shouldn't be defined unless you really, really need them. So, the trick is to call GC.SuppressFinalize in your Dispose method, which stops the Finalizer from running needlessly (I don't know about C#, but if you implement IDisposable in VB.NET, the IDE automatically injects a correct implementation of this pattern for you). I did a quick Google search and found a codeproject article which looks accurate enough and covers it in more detail: http://www.codeproject.com/KB/cs/idisposable.aspx Hope this helps. My knowledge of finalizers is only theory-based. Joe. On Tue, Jun 15, 2010 at 3:34 PM, Bec Carter <[email protected]> wrote: > Hi! > > I have a class which uses temporary files to do its stuff. What do you > recommend for ensuring things get cleaned up when things go bad? > IDisposable? Or perhaps a big try/finally inside the class functions? > > Cheers --Bec-- >
