View Single Post
Old 01-28-2017, 06:23 PM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058

If you have a class that holds onto non-dotnet resources e.g. file handles, database connections and similar for longer than a single method then your class should implement IDisposable and therefore provide a Dispose method. These resources need to be cleaned up in a timely fashion and the Dispose method is a way of allowing an application to free up these external resources at a know point in time, rather than waiting for the garbage collector to kick in and free them up at some point in the future.

Setting a variable to nothing is often unnecessary in .Net - the runtime tracks when a variable is still accessible or not and setting a variable to nothing can actually keep a variable in memory longer than just letting the GC figure it out.

Setting things to nothing can be useful if you wish to free up a single element in an array or similar, it can also help to free up memory if you have variables at the class level that are no longer needed; within a method though it is probably not needed.
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote